Status Update
Comments
il...@google.com <il...@google.com>
il...@google.com <il...@google.com> #2
This is an inherently unsafe thing to ask for (which is why we have no built in support for this use case), but to explain why, I'll need to go into a lot of detail with what 'running suspend code when a lifecycle state is at least X' implies.
Namely: What behavior do you want to have if the Lifecycle falls below your chosen Lifecycle.State
while your suspending work is running?
The way the when
APIs worked is that the work was paused - when you hit a suspension point, your code just wouldn't run anymore. This is purposefully not supported anymore (as it could leave things hanging for a very long time if for instance the user hit the Home button).
So the only other options are (ignoring the case when the Lifecycle is DESTROYED
and the whole coroutine scope is cancelled):
1. Let the suspending code continue to run to completion.
This has the benefit that the entire block runs atomically - i.e., it wouldn't be cancelled half way through.
That approach looks like:
lifecycleScope.launch {
// Suspend until you are STARTED
withStarted { }
// Run your code that happens after you become STARTED here
doYourOneTimeWork()
// Note: your code will continue to run even if the Lifecycle falls below STARTED
}
This type of code comes with the assumption that your one time work does not depend on staying above your state, but that's not something a library could know. For example, if you run a FragmentTransaction
after your suspending work, the state might be saved prior to that call happening.
2. Cancel the suspending code and restart it when you come back above that state.
This is exactly the contract for repeatOnLifecycle
- re-running the block every time you go back above the given State. However, there's no way for a library to know if it is safe for you to re-run the entire block or if you need to checkpoint more often.
In the naive case, where you can run the entire block multiple times until it actually completes successfully, this is just tracking a isComplete
flag:
lifecycleScope.launch {
var isComplete = false
repeatOnLifecycle(Lifecycle.State.STARTED) {
if (!isComplete) {
// Do your work here. It will be canceled if the Lifecycle
// goes down while it is running
doYourOneTimeWork()
// Then mark the work as complete
isComplete = true
}
}
}
Of course, the 'it is safe to rerun the entire block if it gets canceled half way through' is a really big assumption.
3. Cancel the suspending code and don't restart it
This is similar to the previous one, but uses a finally
block to set the isComplete
flag no matter if the work was cancelled or not.
lifecycleScope.launch {
var isComplete = false
repeatOnLifecycle(Lifecycle.State.STARTED) {
if (!isComplete) {
try {
// Do your work here. It will be canceled if the Lifecycle
// goes down while it is running
doYourOneTimeWork()
} finally {
// By marking the work as complete in the finally block,
// we never restart the block just leaving it potentially
// half complete.
isComplete = true
}
}
}
}
I'm struggling to find a valid use case where you want to leave the work only partially finished, so I mostly include this for completeness.
I hope this gives a little more background on this problem space and why only we have the APIs we have - ideally, you'd avoid all of these cases entirely by not trying to run one-time suspending code when a lifecycle state is at least X.
il...@google.com <il...@google.com>
lo...@gmail.com <lo...@gmail.com> #3
Thanks for the detailed response! I think my majority use cases fall into either 1 or 3, i.e. if user initiated an action and then closed the activity/fragment, I would like the action (as a suspend block) to also get cancelled upon lifecycle reaching destroyed. I guess in fact for use case 3, I really just need lifecycleScope.launch
without even needing repeatOnLifecycle
. (In fact, it sounds like launchWhenCreated
acts identically as launch
...)
ja...@gmail.com <ja...@gmail.com> #4
In examples 2 and 3 wouldn't repeatOnLifecycle
continue to be executed every time the lifecycle state gets to STARTED
, even after isComplete
has been set to true
? Is there no way to tell the repeatOnLifecycle function that it can stop repeating?
b9...@gmail.com <b9...@gmail.com> #5
Re launch
to cancel the whole code block if you want. Obviously that only works well if you are doing a naive all or nothing approach and not something where you are check-pointing at multiple points in your suspending block.
ka...@gmail.com <ka...@gmail.com> #6
b9...@gmail.com <b9...@gmail.com> #7
yc...@gmail.com <yc...@gmail.com> #8
il...@google.com <il...@google.com> #9
Cause it might get confusing for people finding that documentation page and then being told that it's gonna be deprecated.
[Deleted User] <[Deleted User]> #10
This issue I was referring to was only reproducible with 27.1.0 though. The one you were referring to was reported prior to 27.1.0 release. Anyway I'll report it as a separate issue. Thanks.
kk...@google.com <kk...@google.com> #11
[Deleted User] <[Deleted User]> #12
an...@gmail.com <an...@gmail.com> #13
I have all the latest stable versions of Android Studio, support libraries, architecture components libraries and so on.
Are you sure that the fix to this issue was included in 27.1.1? Or maybe in 28.0.0-alphaX?
il...@google.com <il...@google.com> #14
You could certainly have an issue with similar symptoms but with a different root cause - if you're able to reproduce an issue even with the latest 28.0.0-alpha3, please file a new bug with a sample project that reproduces your issue.
al...@gmail.com <al...@gmail.com> #15
Have Don't keep activities enabled.
Start the app.
Push home button.
On 27.0.2 I have the following output at logcat:
V/ViewModelFirst: Created
V/ViewModelFirst: onCleared
V/FragmentFirst: onDestroy
V/MainActivity: onDestroy
Which totally correct.
But on 27.1.1 till 28.0.0-alpha3 I have the following output at logcat:
V/ViewModelFirst: Created
V/FragmentFirst: onDestroy
V/MainActivity: onDestroy
As we can see activity and fragment was destroyed but viewModel was not notified with onCleared.
I suspect that in case if the Don't keep activities will be disabled and the app at background will be naturally unloaded by Android at some moment of time the viewModel.onCleared() will not be called which is very sad.
lb...@gmail.com <lb...@gmail.com> #17
al...@gmail.com <al...@gmail.com> #18
3g...@gmail.com <3g...@gmail.com> #20
file sdcard encrypt....
[Deleted User] <[Deleted User]> #21
Common
Description
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation "android.arch.lifecycle:runtime:1.1.0"
implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:common-java8:1.1.0"
Version used: 27.1.0
Theme used: N/A
Devices/Android versions reproduced on: Emulator 24
- Relevant code to trigger the issue.
Make an Activity that contains a Fragment.
Make the Activity use a ViewModel, and make the Fragment also use a ViewModel.
Start the Activity and press back.
The Activity's ViewModel's onCleared method is called.
With AppCompat 27.0.2, the Fragment's ViewModel's onCleared method is called (as it should)
With AppCompat 27.1.0, the Fragment's ViewModel's onCleared method is not called (incorrect).
See the attached project that shows the issue.
With 27.0.2, the logs read:
---------------------------
org.jraf.android.viewmodelproblem.MainActivity: onCreate
org.jraf.android.viewmodelproblem.MainActivityViewModel: constructor
org.jraf.android.viewmodelproblem.MainFragment: onCreate
org.jraf.android.viewmodelproblem.MainFragmentViewModel: constructor
[back is pressed]
org.jraf.android.viewmodelproblem.MainActivity: onDestroy
org.jraf.android.viewmodelproblem.MainActivityViewModel: onCleared
org.jraf.android.viewmodelproblem.MainFragmentViewModel: onCleared
org.jraf.android.viewmodelproblem.MainFragment: onDestroy
With 27.1.0, the logs read:
---------------------------
org.jraf.android.viewmodelproblem.MainActivity: onCreate
org.jraf.android.viewmodelproblem.MainActivityViewModel: constructor
org.jraf.android.viewmodelproblem.MainFragment: onCreate
org.jraf.android.viewmodelproblem.MainFragmentViewModel: constructor
[back is pressed]
org.jraf.android.viewmodelproblem.MainActivity: onDestroy
org.jraf.android.viewmodelproblem.MainActivityViewModel: onCleared
org.jraf.android.viewmodelproblem.MainFragment: onDestroy
This seems to happen because of line 1645 of Fragment, this test:
if (mViewModelStore != null && !mHost.mFragmentManager.isStateSaved()) {
is always false because mHost.mFragmentManager.isStateSaved() is true.