-
Notifications
You must be signed in to change notification settings - Fork 499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle the result of Async when the ViewModel is destroyed #613
Comments
You can use You could add a new value |
Something weird is happening, I've tested it many times and don't know why.
|
You can simplify this to
And make sure to actually annotate your state property with |
I'm sure the
There seems to be nothing special about my code, I added some logs to monitor it
Maybe it has something to do with my testing process:
|
@Kilnn It's pretty hard to help without a complete repro app. Could you make one? |
@gpeal I have create a Sample here. It has only one page to reproduce the problem flow by my test step:
|
My guess is that something in the hilt setup is getting in the way of the normal state restoration from bundle. if you set some breakpoints in that flow you should be able to see what is going on and why it may not be working |
@elihart I tried as you suggested. The result does match the reason I guessed: The data submitted by
The Perhaps there could be a public API that would allow us to manually save when we see fit, such as when |
It's part of the activity/fragment contract that you are not allowed to save any additional state after onPause. Afaik android does not expose any API to do so after onSaveInstanceState. if you have work being done in your viewmodel asynchronously that started while resumed and finished after paused so that it was not saved you may be forced to do something like |
A simple usage scenario, I have a page to modify the password. User enters old and new password and submits request, like this:
And observer the result in fragment, like this:
Normally, it works fine. But when I'm requesting, exit the APP to the background. If after a while, the Activity is destroyed and rebuilt, when I enter again, I will lose the result of the last request.
In this example, after exiting to the background, the
ViewModel
continued to work and successfully changed the password. When I go back again, it still stays on this fragment, which makes the user think that the last password change was unsuccessful.So , how can I handle this scenario? Or, is there an easy way to persist this
Async
Success
state and let me know that the last request was successful.The text was updated successfully, but these errors were encountered: