简体   繁体   中英

Android - event when user backs out of the app?

I'm making an app and I need to store the data on the app when the user backs out of it. Currently, when the user presses the home button the event onPause is called - so I can run a method to save data here.

However, when the user backs out of the app via the back button on the phone the onPause method doesn't seemed to be called so I cannot save any data; upon opening the app again after backing out of it, the last session is lost.

TL;DR: how can I save data when the user backs out of the app? What event is called when the user backs out?

CommonsWare is correct, the onPause() is being called, however I bet the method to load the data into the fragment is only in the onCreate() method.

When you back out, then immediately go back in, onCreate() is not called.

Reference here .

Bottom line, move the populating of the data to onResume(). Let me know how it goes, good luck!

just put your code in OnDestroy(); like this

@Override
protected void onDestroy() {
    super.onDestroy();
            // your code here
}

or the other way to do it

@Override
public void onBackPressed() {
    super.onBackPressed();
            // your code here
}

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