androidactivity-stack

start second activity and fail to back to first activity


Assume that I completely quite my application. (no more activity stack)

However, the notification arrive and user will be navigate to second activity when click

The question is how can I make an second activity back to first activity (lancher) and then exit

instead of exit directly if press back button (because it doesn't have any stack)!?

NOTIFICATION WHICH NAVIGATE TO SECOND ACT.

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_stat_social_person)
                        .setContentTitle("AIR° TRACKING MODE!")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg)
                        .setLights(Color.BLUE, 500, 500)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setOngoing(true);

        PendingIntent contentIntent = PendingIntent.getActivity(
                this,
                0,
                new Intent(this, Activity_Second.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT),
                PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(GM.NOTIFICATION_TRACKING_MODE, mBuilder.build());
    }

Solution

  • TaskStackBuilder is the best choice, I think so...

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(Activity_One.class);
        stackBuilder.addNextIntent(resultIntent);
    

    and in the Manifest:

    work for API < 16

     <activity android:name=".Activity_Second">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Activity_One" />
        </activity>
    

    for API > 16

     <activity android:name=".Activity_Second"
            android:parentActivityName=".Activity_One" />
    

    sorry. when use TaskStackBuilder, remember to edit you PendingIntent like this:

         PendingIntent contentIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );