Jude Fernandes
Jude Fernandes

Reputation: 7517

Unable to create back stack for activities

I am receiving a notification and i want to create a custom back stack so the user can navigate through it.But as of now clicking on the notification opens the desired activity but when i press the back button it completely exits the app.

Intent resultIntent = new Intent(this, NotifViewActivity.class);
    resultIntent.putExtra(StringHolder.NOTIFICATION_ID, notif.getId());

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(HomeActivity.class);
    stackBuilder.addParentStack(NotifActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentTitle(notif.getTitle())
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(resultPendingIntent);

Manifest File

<activity
        android:name=".NotifActivity"
        android:parentActivityName=".HomeActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeActivity" />
    </activity>
    <activity
        android:name=".NotifViewActivity"
        android:parentActivityName=".NotifActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".NotifActivity" />
    </activity>

The way i want it to work is,on click of the notification the user is taken to NotifViewActivity then when back button is pressed the user is taken to NotifActivity and when back button is pressed again the user is taken to HomeActivity .Thats the hierarchy i am trying to create,how can i do that?

Upvotes: 4

Views: 1300

Answers (3)

Samir
Samir

Reputation: 3167

For anyone who is trying to start those the created activity with TaskStackBuilder, follow @Divers solution and then use taskStackBuilder.startActivities().

Upvotes: 0

Divers
Divers

Reputation: 9569

You should build your task stack that way:

    stackBuilder.addParentStack(HomeActivity.class);
    stackBuilder.addParentStack(NotifActivity.class);
    stackBuilder.addNextIntentWithParentStack(resultIntent);

Or actually because you already specifying activity hierarchy in manifest, you can do it with just one line:

   stackBuilder.addNextIntentWithParentStack(resultIntent);

Or another way to archive the same without specifying hierarchy in manifest:

    Intent mainActivityIntent = new Intent(this, HomeActivity.class);
    Intent notifActivityIntent = new Intent(this, NotifActivity.class);
    stackBuilder.addNextIntent(mainActivityIntent);
    stackBuilder.addNextIntent(notifActivityIntent);
    stackBuilder.addNextIntent(resultIntent);

Upvotes: 9

Santosh Bachkar
Santosh Bachkar

Reputation: 460

try this : put below code into NotifViewActivity

 @Override
        public void onBackPressed() {
            Intent i = new Intent(this, HomeActivity.class);
        i.putExtra("exit", true);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        super.onBackPressed();
        }

Upvotes: -2

Related Questions