4

I want to preserve the order of navigation, so I followed this:

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

and this:

Android - Build a notification, TaskStackBuilder.addParentStack not working

Once the notification is clicked, ResultActivity is opened, and if the user click Back, MainActivity appears.

When I click back, it close the app and return to Home screen instead of openning the MainActivity. However, when I reopen it, it has already started with MainActivity. Here's my code:

Manifest:

<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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".AlarmReceiver" />

        <activity
            android:name=".ResultActivity"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>

OnReceive in AlarmReceiver

        builder.setContentTitle("sth");
        builder.setContentText("sth");

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        // Back button
        stackBuilder.addParentStack(ResultActivity.class);
        // The desired activity when clicking notifications
        stackBuilder.addNextIntent(new Intent(context, ResultActivity.class));

        // Create Pending Intent
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);

        // Finally a manager
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());

How to fix this? Thanks!

Community
  • 1
  • 1
Minh Nghĩa
  • 854
  • 1
  • 11
  • 28

0 Answers0