How to Make an Android App Return to the Last Open Activity When Relaunched

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

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

How to return to the latest launched activity when re-launching application after pressing HOME?

Oh, I think I've found the answer.

Because I was launching the app by using IntelliJ, it seems it launches the application in a different way than a user, clicking a home screen widget, would launch. It's explained in the answer to another SO question:

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

Ref: App always starts fresh from root activity instead of resuming background state (Known Bug)

So I've manually killed the application in the phone, and relaunched it again from the home screen widget. Then opened the GameActivity, and pressed HOME. Now, when relaunching it the GameActivity is still visible and keeping its UI state as it was when I left it.

And I guess that the reason why a new instance of the activity was created when pressing the shortcut before was due to a different Intent being used for starting the activity.

android back to last opened activity when click on app icon

In your Launcher activity onCreate() method, write this:

if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {

finish();
return;
}

isTaskRoot() is true when there are no activities in activity stack. In your case, if another activity (Third activity in your case) is there in Activity stack, above code will finish the launching activity and will bring Third Activity to screen.

How to save data and re-open the last used Activity


Why does it happen?

The Bundle created in onSaveInstanceState is only kept for short-term restarts, e.g. when your activity is recreated due to an orientation change. If your activity stops normally this Bundle is not kept, and thus can't be delivered to onCreate.

What can I do to fix this?

Instead of using the instanceState model, save the activity state in SharedPreferences (similar to the lastActivity which you already save).
You should save your state in onStop or onPause and restore it in onStart or onResume respectively. In most cases the former is enough and the latter produces only unnecessary overhead.


@Override
protected void onStart() {
super.onStart();
String saved = sharedPreferences.getString("myKey", null);
MyPojo pojo;
if(saved == null){
pojo = defaultValue;
}else {
pojo = new Gson().fromJson(saved, MyPojo.class);
}
}

@Override
protected void onStop() {
sharedPreferences.edit().putString("myKey", new Gson().toJson(pojo)).apply();
super.onStop();
}

How to return to last activity after recents screen

Do not use android:launchMode = "singleInstance", it will create a new task.

Start same android last activity on relaunch after closing the app

By default last activity will open when relaunch the app.But some device it will not happen.
I don't know the proper reason but may there is settings problem in that device.



Related Topics



Leave a reply



Submit