Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: BackStack misbehaves, if application was started from another app

I have an application, which starts with a SplashScreenActivity. Afterwards, a LoginActivity is shown, or if the user is already logged in, a MainActivity is shown. If the application is already running, SplashScreenActivity is dismissed 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;
        }
    }

Problem occurs

If I start the application from another activity like PlayStore, it resumes at the right activity if already running. This is the Intent I'm using to reproduce within a second app

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

However, this action is somehow breaking the Backstack. Instead of closing the application on backpress in the 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 out that MainActivity:onDestroy() get called, but instead of resuming application to the HomeScreen, it always restarts and I don't know why.

I tried the following: - Used different launchmodes like singleTask and singleInstance, but it didn't make any difference. onNewIntent is called, but if i call finish, HomeActivity restarts - as commeted below, i tried moveTaskToBack(true), but Activity is restaring too (and we really want to close the app instead of moving it to the BackStack)

like image 993
longi Avatar asked Aug 06 '15 15:08

longi


People also ask

What is Android back stack?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.

What is task affinity Android?

▶ Task affinity specifies which task that the activity desires. to join. By default, all activities in an app have the same. affinity – the app package name. <manifest xmlns:android="http://schemas.android.com/apk/res/android"


1 Answers

Try with moveTaskToBack(true); instead of finish(); to close the app. It will then go to OnRestart() and then OnStart()->OnResume() (and won't go to OnCreate).

And make sure you don't have the "Don't keep activities" marked at Developer Options in your Android Settings (destroy every activity as soon as the user leaves it).

like image 187
Joanmi Bardera Avatar answered Oct 19 '22 08:10

Joanmi Bardera