How do you build an Android back stack when an activity is started directly from a notification?

StackOverflow https://stackoverflow.com/questions/7113766

Вопрос

I have two activities:

Activity A - list of items
Activity B - detail view of an item

Normally, a user opens the app and Activity A is launched. A user sees a list of items, clicks one, and Activity B is started to display the item detail.

Activity B can also be started directly from clicking on a notification. In this case there is no back stack.

How can I make it so that when Activity B is started directly from a notification, the user can click the Back button and go to Activity A?

Это было полезно?

Решение

You can add an Extra into the Intent launched by the notification to detect when the app has been launched in that way.

Then you can override the onBackPressed() Activity method and handle that scenario, e.g.

  @Override
  public void onBackPressed()
  {
    Bundle extras = getIntent().getExtras();

    boolean launchedFromNotif = false;

    if (extras.containsKey("EXTRA_LAUNCHED_BY_NOTIFICATION"))
    {
      launchedFromNotif = extras.getBoolean("EXTRA_LAUNCHED_BY_NOTIFICATION");
    }

    if (launchedFromNotif)
    {
      // Launched from notification, handle as special case
      Intent intent = new Intent(this, ActivityA.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      mActivity.startActivity(intent);
      finish();
    }
    else
    {
      super.onBackPressed();
    }    
  }

Другие советы

You should take care of this when you receive the Notification.

I have a similar situation solved:

 Intent intent = new Intent(context,ListDetail.class);
 TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
 stackBuilder.addParentStack(ListDetail.class);
 stackBuilder.addNextIntent(intent);
 PendingIntent contentIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
 NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 Notification.Builder mBuilder = new Notification.Builder(context);
 mNotifM.notify(NotificationId.getID(), mBuilder.setStyle(new Notification.BigTextStyle(mBuilder)
            .bigText(bigText)
            .setBigContentTitle(title)
            .setSummaryText(summaryText))
            .setContentTitle(title)
            .setSmallIcon(icon)
            .setContentText(summaryText)
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setTicker(bigText)
            .build());

You need to set in your Manifest the hierarchy of the Activities:

<activity
        android:name=".ListDetail"
        android:label="Detail"
        android:parentActivityName=".List" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".List" />
</activity>

I have tried one sample.Please go through with this link

https://github.com/rajajawahar/NotificationBackStack

Activity you want to launch..

Intent launchIntent = new Intent(context, SecondActivity.class).putExtra("Id", id);

Parent Activity, if back pressed

Intent parentIntent = new Intent(context, FirstActivity.class);

Add Both the activity in the taskbuilder

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    PendingIntent resultPendingIntent = stackBuilder.addNextIntentWithParentStack(parentIntent).addNextIntent(launchIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationCompatBuilder =
                new NotificationCompat.Builder(context);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(id, notificationCompatBuilder.build());
        NotificationManager mNotifyMgr =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationCompatBuilder.setAutoCancel(true).
                setContentTitle("First Notification").
                setContentText("Sample Text").
                setSmallIcon(R.mipmap.ic_launcher).
                setContentIntent(resultPendingIntent);
        mNotifyMgr.notify(id, notificationCompatBuilder.build());

catch the back-button-key event with onKeyDown()-method and let the user go to activity A. Don't forget to return true to prevent the event from being propagated further.

http://developer.android.com/reference/android/app/Activity.html#onKeyDown(int, android.view.KeyEvent)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top