Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an android app return to the last open activity when relaunched?

Tags:

android

Is it possible to configure an android app so that if a user has opened your app, launched numerous activities, then returns to the home screen and relaunches your app again, instead of going to the main activity they will instead be taken to the activity highest on the stack (the most recent activity in your app)?

like image 498
JohnRock Avatar asked Mar 14 '10 04:03

JohnRock


People also ask

How do you start an activity only once the app is opened for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

How do I go back to first activity on Android?

Declare A in your manifest with the android:launchMode="singleTask" . This way, when you call startActivity() from your other activies, and A is already running, it will just bring it to the front.

How does Android know which activity to run first?

Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context. startActivity(). So, CATEGORY_DEFAULT can appear number of times. Android does not grab whichever one appears first in the manifest but it starts with activity having CATEGORY_LAUNCHER.


2 Answers

When launched via icon on the home screen, Android will always start the activity with the android.intent.action.MAIN filter in your AndroidManifest.xml, unless the application is already running (in which case it will obviously restore the activity on top of the stack).

To achieve what you described you can simply store the last visible activity in SharedPreferences and have a Dispatcher activity that starts the last activity according to the preferences.

So in every activity you want to re-start automatically:

@Override protected void onPause() {     super.onPause();      SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);     Editor editor = prefs.edit();     editor.putString("lastActivity", getClass().getName());     editor.commit(); } 

And a Dispatcher activity similar to the following:

public class Dispatcher extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          Class<?> activityClass;          try {             SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);             activityClass = Class.forName(                 prefs.getString("lastActivity", Activity1.class.getName()));         } catch(ClassNotFoundException ex) {             activityClass = Activity1.class;         }          startActivity(new Intent(this, activityClass));     } } 

Remarks

  • You could create a base class for the onPause override
  • The Dispatcher activity obviously needs to be the android.intent.action.MAIN action
like image 187
Josef Pfleger Avatar answered Oct 10 '22 00:10

Josef Pfleger


It's not that complex. You just need to manipulate the manifest.

AndroidManifest.xm

<activity      android:name=".MainActivity"      android:alwaysRetainTaskState="true"      android:exported="true"      .      .      . 

Read about the 'android:exported' & 'android:alwaysRetainTaskState' here:

android:exported

android:alwaysRetainTaskState

like image 25
Shayan_Aryan Avatar answered Oct 09 '22 22:10

Shayan_Aryan