TaskStackBuilder not working correctly when launch activity twice from home screen

652 Views Asked by At

I have 2 Activities

  • MainActivity - with 4 tabs

  • ContactActivity

I make two intents in TaskStackBuilder so the ContactActivitywill appear first and after it MainActivity when i click on notification.

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

stackBuilder.addNextIntent(getIntentToMainActivity(context)); // Added extras for tab 1.

final Intent intent = ContactActivity.createIntent(context, 0, phone, false);
intent.putExtra(EXTRA_MISSED_CALL_NOTIFICATION_CLICKED, true);
stackBuilder.addNextIntent(intent);

notificationBuilder.setContentIntent(stackBuilder.getPendingIntent(1000, FLAG_UPDATE_CURRENT));

Scenario:

  1. Click on notification open the ContactActivity correctly.

  2. Press home button.

  3. Click from homescreen on my launcher that open the MainActivity in tab 2.

What that happening now is that the MainActivity opened with tab 1 with the extras of tab 1 that set from stackBuilder (probably from stackBuilder).

Manifest MainActivity:

<activity
    android:name=".MainActivity"
    android:clearTaskOnLaunch="true"
    android:finishOnTaskLaunch="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/contactlist_app_name"
    android:launchMode="singleTask"
    android:screenOrientation="user">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
1

There are 1 best solutions below

0
On BEST ANSWER

I added to the subActivity flag:

 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Then its work for me.