Issue
I asked this question Is onDestroy called only if you explicitly call finish() ?? or are there any exceptions? now in answer I got that
where are the cases where ondestroy() not get called up.
If you crash with an unhandled exception
If your process is terminated in an urgent fashion (e.g., the system needs RAM to process an incoming phone call)
If the user clicks "Force Stop" on your app's screen in Settings
On a few devices, if the user terminates your process using a manufacturer-supplied task manager
Now as for these cases as onDestroy()
will not get called so I thought to try onStop()
But as for all above cases even if i will write code in onStop()
to unregister
the receiver then still it will not get called because of which my receiver will left registered.
So now my question is where can i write my code to unregister the receiver when any of the above four cases will happen.
Also if it is not possible then i guess as for both
onStop()
andonDestroy()
for these four cases we cannot rely on them to unregister our receiver then why in Android docs it is written to not useonDestroy()
even both are equally unreliable ??
Shouldn't they say that both functions should not be used for releasing resources(unregistering receivers)
.
Solution - According to commonsware answer
In all three of these cases, your process is gone, and therefore your BroadcastReceiver is also gone. There is nothing to unregister.
So as the broadcastreceiver is also gone, so there won't be any need to unregister the receiver, So i think there won't be any problem in all these three cases if i will use onDestroy()
to unregister
the receivers.
Only for the 1 Case
i will try to implement my own top-level uncaught exception handler, as onDestroy() won't be called for that.
Solution
But as for all above cases even if i will write code in onStop() to unregister the receiver then still it will not get called
For three of the above cases, in the vast majority of cases, onStop()
will be called:
If your process is terminated in an urgent fashion (e.g., the system needs RAM to process an incoming phone call)
If the user clicks "Force Stop" on your app's screen in Settings
On a few devices, if the user terminates your process using a manufacturer-supplied task manager
Android will not terminate a process that is in the foreground from a UI standpoint. In all of these cases, the vast majority of the time, your app will not be in the foreground, and therefore onStop()
will have been called.
Moreover, in all three of these cases, your process is gone, and therefore your BroadcastReceiver
is also gone. There is nothing to unregister. This is one of the reasons why tend not to worry too much about these scenarios. onDestroy()
is for cleaning up things that might prevent your activity from being garbage-collected, and if your process is terminated, your activity and all other objects are all gone.
The one scenario from the four that remains is if your app crashes with an unhandled exception. In that case, your app is seriously messed up. If you have your own top-level uncaught exception handler, you might consider terminating your own process as part of cleanup.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.