简体   繁体   中英

Preventing from onDestroy being called screen off button pressed

I have Activity that gets populated with info. When I press the screen off button and come back to the Activity, the info is not there anymore.

Btw, If I use the home button the info doesn't disappear. On logs, it shows it doesn't call onDestroy;

Therefore, onDestroy discards the info. I don't want onDestroy. HOw could I make that happen.

Thanks.

OnDestroy will always be called. You should save data and than recreate Activity on onCreate. Everything is nicely explained here . Read section "Saving Persistent State". Also read about lifecycle of Activity.

Try to save activity's info on onSaveInstanceState() and restore the state on onRestoreInstanceState() . This approach also helps you also in configuration changes state. onSaveInstanceState() is called before destroying activity and onRestoreInstanceState() is called just before recreating the activity. Follow this for more details.

Probably duplicate of Android activity onDestroy() called on screen lock .

This behaviour usually occurs at landscape orientation activities. Add

android:configChanges="orientation|keyboardHidden|screenSize"

to your AndroidManifest.xml activity tag.

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