Android TaskStackBuilder.startActivities() doesn't work as expected

3.3k Views Asked by At

Possible douplicate of Android - Build a notification, TaskStackBuilder.addParentStack not working

Although there are a lot of questions answered about this topic but none of them solved my problem. I'm actually, playing with all of them but couldn't get them into work :(

I have a Splash Screen, I'm checking a flag (I get it from backend) and navigate user to SecondActivity, for example, instead of FirstActivity if flag is true.

My Manifest is like this:

<activity
            android:name="com.package.name.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.package.name.Firstctivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.package.name.SecondActivity"
            android:label="@string/title_tracking"
            android:screenOrientation="portrait"
            android:parentActivityName="com.package.name.Firstctivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.package.name.Firstctivity" />
        </activity>

I'm calling following method in onResume() method of SplashScreen Activity:

private void checkFlag()
    {
        ...

        if (flag == null)
        {
            return;
        }

        // show tracking screen
        final Intent intent = new Intent(SplashActivity.this, SecondActivity.class);
        intent.putExtra(SecondActivity.EXTRA_ID, "1234");
        intent.putExtra(SecondActivity.EXTRA_ACTION, SecondActivity.ACTION_VISITING);
        intent.putExtra(SecondActivity.EXTRA_LIVE_STATUS, true);

        TaskStackBuilder sBuilder = TaskStackBuilder.create(SplashActivity.this);
        sBuilder.addNextIntent(new Intent(SplashActivity.this, FirstActivity.class));
        sBuilder.addNextIntent(intent);
        sBuilder.startActivities();

        SplashActivity.this.finish();
    }

Now, when user launches the app and flag is not null then he will be directed to SecondActivity. However, by click on back button application closes but I expect to see FirstActivity instead. In SecondActivity, by click on up carret user navigates to FirstActivity which I expect to see same behavior if user clicks on back button.

Any suggestion would be appreciated. Thanks.

2

There are 2 best solutions below

0
On

Although Up navigation was working fine (based on my above code), Back button was not following same way. I'm not sure there is a bug in TaskStackBuilder or it's my problem however this is the method I used to fix my problem. The reason that I found is because back button navigates in chronological order through the history. Since there is no activity before that then application closes.

I added another flag into my intent intent.putExtra(SecondActivity. EXTRA_BACK_MAIN_ACTIVITY, true); and following code into my SecondActivity:

@Override
public void onBackPressed()
{
    final Bundle extras = this.getIntent().getExtras();
    if ((extras != null) && extras.containsKey(SecondActivity.EXTRA_BACK_MAIN_ACTIVITY))
    {
        setupNavUtils();
    }
}

private void setupNavUtils()
{
    final Intent intent = new Intent(this, FirstActivity.class);

    if (NavUtils.shouldUpRecreateTask(this, intent))
    {
        TaskStackBuilder.create(this).addNextIntent(intent).startActivities();
        super.finish();
    }
    else
    {
        NavUtilsLegacy.navigateUpTo(this, intent);
    }

}
0
On

Set the launchMode of the FirstActivity to singleTop.