简体   繁体   English

Android-用户退出应用程序时发生的事件?

[英]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. 当前,当用户按下主页按钮时,将调用onPause事件-因此我可以在此处运行一种方法来保存数据。

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; 但是,当用户通过电话上的“后退”按钮退出应用程序时,似乎未调用onPause方法,因此我无法保存任何数据。 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? TL; DR:当用户退出应用程序时如何保存数据? 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. CommonsWare是正确的,onPause()被调用,但是我敢打赌,将数据加载到片段中的方法仅在onCreate()方法中。

When you back out, then immediately go back in, onCreate() is not called. 当您退出时,然后立即返回,不会调用onCreate()。

Reference here . 参考这里

Bottom line, move the populating of the data to onResume(). 最下面的一行,将填充的数据移动到onResume()。 Let me know how it goes, good luck! 让我知道进展情况,祝您好运!

just put your code in OnDestroy(); 只需将您的代码放入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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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