简体   繁体   中英

Android How to get Last Focused Activity after "Force Stop" the Application

I am developing android Application having number of activities. The requirement of the application is When i "FORCESTOP" the application manually(Settings->Applications->ManageApplication->APP_NAME->FORCESTOP), the last focused activity(UI Page) to be displayed. Can i get the Last focused Activity after *FORCESTOP *ing the Application? Please help me with the sample code.

You should keep track of each activity change and save it to file (eg in your sharedpreferences) immediately when the change happens. Something like this

protected void onResume() {
    SharedPreferences.Editor edit =  PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).edit();
    edit.putString("LastActiveActivity", this.getClass().getName());
    edit.commit();
    super.onResume();
}

Make sure you save the current activity in the onResume() method of all your activities.

When you restart you can read the last activity back from your preferences. This should be done in the onCreate() method of your main activity (the one that is specified as "LAUNCHER" in your manifest). Then start the activity with the name you've read back.

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