Issue
I have an application that plays audio and video files from the remote server or from its own cache directory. When I minimize my application with "home" or "back" button it changes its state to onStop(). Then, after some idle (20-50 minutes or about) Activity Manager sends "No longer want" message to the activity and closes it.
According to Activity Lifecycle in this case the aplication jumps to onCreate (the branch to the left from onStop() on the diagram). When it happens my activity has an incorrect representation - fragments with track lists start to load on top of existing ones, UI becomes unusable and the only way to fix this situation is to competely restart the application.
I guess there are two ways to solve the problem:
1) Make the system not to kill my application (may be it's not correct, because this approach violates normal application lifecycle in Android and accelerates the battery discharge).
2) Detect the situation when OS kills my application and clean resources to let OS create a new activity properly.
I think the second variant is preferable.
Is there some ideas how to implement it? It is possible to detect that Android is going to kill my application and react to this action?
Thanks in advance!
Solution
If your app creates a service that does nothing in idle state, it will not drain extra battery and it will pin your process (you should also put your notification to make your claim stronger). But whatever you do, the system may destroy your app when it is low on resources. Note that the only callback that you are guaranteed to receive is
onPause()
, and even then the system will get very upset if it takes too much time to complete. All other calls - like onStop() and even finalizers - may be skipped if the system is desperate.Therefore, you must also follow the second strategy - which is IMHO what Simon's comment actually means - and handle
onStart()
andonRestart()
correctly.
Answered By - Alex Cohn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.