'Is there any way to guarantee that the onPause or onStop Android lifecycle events run until the end?
In the Android documentation, it's stated that, at least, onPause is not killable. Maybe my interpretation is incorrect but I understood that it would always run until the end. I've made several tests with a lengthy loop cycle and I can interrupt it half call.
I want to save some persistent data in the call but unfortunately, it can be interrupted halfway through. I'm aware I should optimize it and I'm going to do it but I wanted to guarantee that the lifecycle call I'm using would run until the end.
Solution 1:[1]
If those events are raised the only way to "brake" them is by throwing an Exception before their end. Saying that "onPause/onStop events are not killable" only means that this below code (pseudo-code) will always execute YOUR CODE:
@Override
public void onPause/onStop() {
try {
--------------
YOUR CODE HERE
--------------
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
} finally {
super.onPause/onStop();
}
}
However Android Docs says that AFTER these methods the Process could be killed in any time, but the onPause/onStop events will be executed from their begin to their end in any case.
If you want to be secure to save persistant data on those ASYNC events, I suggest you to spawn a different Process and pass "serialized" (using AIDL?) objects to it. Obliviously this spawned new Process is reserved to only this kind of execution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
