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)