简体   繁体   中英

Resume after App kill Android

I am designing an Android App.

My aim is that if the App is killed while it was in the background, and if the user starts the app again then it should have the option to resume the App.

Now, my onStart function is called in two scenarios:

  1. When the activity is started for the first time
  2. Even if the activity started after the kill.

I want to check which can determine in the onStart() , that the app started from scratch, or it is resuming from previous state?

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

What you can do.Create a Shared Preference variable inside onDestroy method

And check of the variable if it is present .On resuming the previous state it wont be present.It will be present only when the activity is being destroyed

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