简体   繁体   中英

The activity loses focus when home key button is pressed on Android

When I press the home button on my phone, and when I return to the application, it loses the focus completely, it loses track of where activity was; ie the Activity returns to principal state. How to avoid that so that when I press the home key and return to the app; to return to the where the activity was?

I need the solution to work for all devices?

Thanks.

Well when you press the home button, the activity's lifecycle methods will be called (first onPause() , then onStop() ), and then when you return, if in the meantime your operating system didn't call onDestroy() (this happens when you force close an app or are running out of memory), your Activity will call onResume() method. If it was destroyed, it will call onCreate and start from the beginning.

So basically what you need to do is override ( CTRL+O in Android studio to open the override menu) these methods to save your application state, save the important parameters of your app (since you've not shared any code, I cannot presume what these are) and to then later restore the state your app had previously come to.

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