2

Hi team i am facing an issue in notification backstack

say i have 4 activities A1,A2,A3,A4

I have a notification which is intent to A2, suppose imagine that we are in A4 went via A1 -> A2 -> A3 ->A4, now i m receiving notification and while tapping notification i am intended to A3, when i back press in this scenario my navigation stack was cleared and app is getting closed.

But my expected behaviour in this scenario was it should go to A4 and the A3, A2,A1 on backing button pressing

code

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle("Notification Alert, Click Me!");
    mBuilder.setContentText("Hi, This is Android Notification Detail!");
    Intent resultIntent = new Intent(context, SecondActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FirstActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    //PendingIntent resultPendingIntent = PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

// notificationID allows you to update the notification later on.
        mNotificationManager.notify(14, mBuilder.build()); 

Manifest file

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        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=".FirstActivity" >
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:parentActivityName=".FirstActivity"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <activity
        android:name=".ThirdActivity"
        android:label="@string/title_activity_third"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
</application>
Suresh
  • 1,199
  • 2
  • 12
  • 36

1 Answers1

0

You are using the wrong parameter for addParentStack(). You should see the activity you open from the notification as the parameter, not it's parent. So change your code to:

stackBuilder.addParentStack(SecondActivity.class);

If your notification is for A2, it will always open A2, never A3.

android:parentActivityName was introduced in API level 16. If you are running this on an older phone, you have to define the parent activity as meta-data as well:

<activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:parentActivityName=".FirstActivity"
        android:theme="@style/AppTheme.NoActionBar" >
 <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".FirstActivity" />
</activity>
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71