Back to main activity from notification-created activity

后端 未结 9 2109
粉色の甜心
粉色の甜心 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:21

    just add this lines to your manifest.

    <activity
        android:name=".YourActivity"
        android:launchMode="singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true">
    </activity>
    

    for a complete guideline refer to https://developer.android.com/training/notify-user/navigation

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 05:25

    I know I'm late but this may help someone.
    Just create an intent for the activity you want to open with the notification. Then set Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TASK as flags.
    Then all you need to do is to create a PendingIntent with your intent as parameter.

    This is a code example:

    Intent intent = new Intent(this, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
    0 讨论(0)
提交回复
热议问题