简体   繁体   中英

How to restart my app in the activity that was running before system kills it android?

我的应用程序的用户大部分时间在后台运行它。很长一段时间后,系统将我的应用程序杀死,当他们尝试进入我的应用程序时,它又重新开始。如何获取应用程序的最后一点被杀?

Override Activity.onPause() and in there put some code that saves to a database or SharedPreference the current activity's class name. Then from your main activity read back from the database or SharedPreference and start the appropriate activity.

You can use shared preferences for tasks like these.

In your onStop() function, set a flag in shared preferences like:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("killed", "yes");

In your onStart(), retrieve the preference and check it like this:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String flag = prefs.getString("killed", null);

if(flag!=null && flag.equals("yes")
{
//activity is resumed
}
else
{
//activity is started from scratch
}

for more info on shared preferences, see official docs here: http://developer.android.com/guide/topics/data/data-storage.html#pref

**This was on a similiar post called "Resume after App kill Android" Here

The answer belongs to - Anup Cowkur**

The only guarantee that you have is that onPause() will be called when the Activity goes into the background. Before Android kills your app, there is no guarantee that onStop() or onDestroy() will be called . What you can do is to write the state of your app, your settings, important data, etc., into either a SharedPreferences file or a database and retrieve this in onResume().

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