Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-How to detect on resume that my app was killed by VM?

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?

like image 913
Khawar Raza Avatar asked Jan 17 '13 11:01

Khawar Raza


People also ask

How do you know if an application is killed?

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.

Which method is called when app is killed android?

When Android decides to kill our app, our activities will call onDestroy method.

How is an activity killed in Android system?

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.

What are KR errors in Android Apps?

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.


1 Answers

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.

like image 157
bwt Avatar answered Sep 24 '22 13:09

bwt