Android: BackStack fails if an application was launched from another application

I have an application that starts with SplashScreenActivity . LoginActivity then displayed, or if the user is already registered, MainActivity displayed. If the application is already running, SplashScreenActivity rejected with the following

 //SplashScreenActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Adding this check for following cases if (!isTaskRoot()) { String intentAction = getIntent().getAction(); if (getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) { finish(); return; } if(getIntent().getCategories().contains(GCMIntentService.INTENT_CATEGORY_GH_NOTIFICATION)){ finish(); return; } } 

There is a problem

If I launch the application from another type of activity, such as the PlayStore, it resumes with the correct action if it is already running. This is the Intent that I use to play in the second application

 //AnotherApplication.apk Intent launchIntent = getPackageManager().getLaunchIntentForPackage("my.package.name"); startActivity(launchIntent); 

However, this action somehow breaks the backstack. Instead of closing the application when you click MainActivity it restarts the application.

 //MainActivity.class @Override public void onBackPressed() { if (getNavDrawerMain().isDrawerOpen()) { getNavDrawerMain().closeDrawer(); } else { closeApp(); } } protected void closeApp() { if (doubleBackToExitPressedOnce) { //super.onBackPressed(); //i tried both, but behaviour is the same finish(); return; } this.doubleBackToExitPressedOnce = true; new Handler().postDelayed(new Runnable() { @Override public void run() doubleBackToExitPressedOnce = false; } }, 500); } 

I used breakpoints and found that MainActivity: onDestroy () is being called, but instead of resuming the application on HomeScreen, it always restarts, and I don’t know why.

I tried the following: - Different start-up layouts were used, such as singleTask and singleInstance , but that did not matter. onNewIntent is called, but if I call finish , HomeActivity restarts - as described below, I tried moveTaskToBack(true) , but the activity also works (and we really want to close the application, and not move it to BackStack)

+6
source share
2 answers

Trying to add this flag to your intent by starting your application: RESET_TASK_IF_NEEDED, URL = http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

What does he do:

If it is set, and this activity either starts in a new task or displays an existing task at the top, then it will be launched as the entrance door to the task.

You can also use: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

What does he do:

If set in the Intent passed to Context.startActivity (), this flag will cause the launched activity to be transferred to the front of the task history stack, if it is already running.

Which one you use depends on the desired end result.

If you cannot control who runs you, you need to set or run the mode for a single task or single instance.

Described here:

http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

The interesting part:

singleTask

This mode is very different from standard and singleTop. Activity with singleTask launchMode is allowed to have only one instance in the system (aka Singleton). If an existing Activity instance exists in the system, the entire Task holds the instance, which will be moved to the beginning while the Intent is delivered using the onNewIntent () method. Otherwise, a new activity will be created and placed in the desired task.

+1
source

Try with moveTaskToBack(true); instead of finish(); close the application. Then it will go to OnRestart (), and then OnStart () -> OnResume () (and not go to OnCreate).

And make sure that you do not have “Do not perform actions” marked in the “Developer options” in the Android settings (destroy all actions as soon as the user leaves them).

+1
source

All Articles