In my app, when user presses HOME key and returns back to app after some time, my app gives NullPointerExceptions on various places and activities. I know that my app is being killed by OS to free some resources. Now I want that when user comes back again to app and app was previously killed, then how can I detect that my app was killed so that I can reload different resources?
for some cases, you can create splash screen page to determine if app has been killed previously by system. it basically a screen with intent filter main/launcher, and will be finished, and changed to main activity.
When Android decides to kill our app, our activities will call onDestroy method.
Activity can't be killed but Os can kill the whole application. In this case you can try finish() / finishActivity() / context. finish() to finish the activity. While you finish an activity backpress will not return to the previous activity.
The app contains a KR error: if the app is restarted, the user's selection is not saved. In Figure 2 we show two screen shots: on the left, we have the app screen after the user has selected a date (11. March 2016); the user naturally expects that the selection will survive a resume or restart.
When onCreate (Bundle savedInstanceState)
is called, check that savedInstanceState
is not null. As stated by onCreate :
savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.
See also onSaveInstanceState() which is not part of the normal lifecycle but is called before the activity is killed.
Note : if the user does A ----> B ----> C --back--> B ----> C, the second time C is created the passed bundle will be null
, as it is a new instance, not the same recreated after being killed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With