简体   繁体   中英

Android Programming: onPause() called, onSaveInstanceState(…) not

I'm in the process of learning android.The book I'm reading seems to be saying that if an activity is paused, the onSaveInstanceState(...) method will be called.

When I hit the home button, onSaveInstanceState(...) is called after onPause() is called. LogCat shows onPause() called, onSaveInstanceState called and then onStop() called.

When I hit the back button, LogCat shows onPause() called, onStop() called, onDestroy() called. onSaveInstanceState(...) is not called.

Should onSaveInstanceState(...) always get called after onPause() is called? If not, why does it get called after onPause() when I hit home and not after onPause() when I hit back?

Thanks!

Greg

onSaveInstanceState() is called when:

  • Hit the home button
  • Oientation of the device changes
  • When you switch your app with another one (choose another app from the list)

If you finish your activity (programmatically or hitting back button), then onSaveInstanceState() is not called.

For onRestoreInstanceState() see this post:

Android: onSaveInstanceState not being called from activity

onSaveInstanceState(...) is called when you hit home button because, you can save bundle at that particular point and return back to the same point where you left your app

for Example if you are reading news in some news app and you are half way through that entire page and now you want to hit home button use some other app and come back to read news you would be expecting to read news from where you left you don't want to start scrolling from top write, so when user click on home button ONsaveInstance is called where you can save view point of bundle resume the view from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM