Skip to content Skip to sidebar Skip to footer

How To Start The Activity That Is Last Opened When Launch An Application?

I designed an app with several activities. There is only an activity instance in the back stack at any time. When I quit the application from an activity named AcitivityOne, how co

Solution 1:

The fast method, is that in your onCreate() put those flags after setContentView():

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
    finish(); 
    return; 
} 

Also you can create a SharedPreferences with the Activity last oppened as follows :

Modify your onPause() to this :

@OverrideprotectedvoidonPause() {
  super.onPause();
  SharedPreferencesprefs= getSharedPreferences("MyPref", MODE_PRIVATE);
  Editoreditor= prefs.edit();
  editor.putString("lastopened", getClass().getName());
  editor.commit();
}

And then in your onCreate() again you put this :

Class<?> LastOpened;

    try {
        SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
        LastOpened= Class.forName(prefs.getString("lastoppened", MainActivity.class.getName()));
    } catch(ClassNotFoundException ex) {
        LastOpened= MainActivity.class;
    }

    startActivity(new Intent(this, LastOpened));

If it doesn't help take a look at this thread

Post a Comment for "How To Start The Activity That Is Last Opened When Launch An Application?"