Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState() should be in onDestroy() rather than onStop()?

Tags:

android

Why does the system call onSaveInstanceState() when the activity enters the stop state?

Since the activity's instance s kept resident in memory when it is stopped, and its current state is lost only when the activity is destroyed.

So the system should rather call the onSaveInstanceState() before entering or inside onDestroy() rather than onStop().

Isn't it?

like image 896
user2882662 Avatar asked Nov 05 '13 16:11

user2882662


People also ask

Is onPause always called before onStop?

onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause() . onStop() may be called after onPause() , or it may not.

When onDestroy () is called before onPause () and onStop () in an Android application?

onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.

What is the difference between the onPause () method and the onSaveInstanceState () method?

From the documentation I understand that onSaveInstanceState() should be called to store only temporary information, and onPause() should be used to store any persistent data.

Is onSaveInstanceState always called?

I'm aware that onSaveInstanceState() is not always called when an activity is about to be destroyed. For example, if it is destroyed because the user has pressed the "back" button, the activity state is not preserved.


1 Answers

Because onDestroy() is not guaranteed to be called (generally, only if you finish() your activity) and because after onStop(), your activity is not guaranteed to remain. It might be destroyed for any number of reasons once it is not the foreground activity.

I always assume that after onPause(), the next callback to be made might be onCreate(), that way, I never get surprised ;)

like image 170
Simon Avatar answered Sep 24 '22 18:09

Simon