Back to main activity from notification-created activity

后端 未结 9 2115
粉色の甜心
粉色の甜心 2020-11-30 04:33

I\'m trying to implement the behaviour described here, where a notification (or whatever) starts an \"internal\" activity in your app, and then when the user pressed back it

9条回答
  •  眼角桃花
    2020-11-30 05:24

    Intent displayIntent = new Intent(getApplicationContext(), TargetActivity.class);
    
    displayIntent.putExtra("extra_id", "extra_key");
    
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    stackBuilder.addParentStack(TargetActivity.class);
    stackBuilder.addNextIntent(displayIntent);
    stackBuilder.editIntentAt(1).putExtra("extra_id", "extra_key");
    
    PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            getApplicationContext(), "123").setSmallIcon(R.drawable.logo)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Sample")).setContentText("sample text");
    
    mBuilder.setContentIntent(contentIntent);
    
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(456, mBuilder.build());
    stackBuilder.startActivities();
    

    In addition to the above code also provide appropriate parent class names in your AndroidManifest.xml file.

提交回复
热议问题