Back to main activity from notification-created activity

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

    Aha, I simply need to use PendingIntent.getActivities() instead of getActivity(). Also worth mentioning is TaskStackBuilder

    Here is some code:

        Intent backIntent = new Intent(ctx, MainActivity.class);
        backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
        Intent intent = new Intent(ctx, ConversationActivity.class);
        intent.putExtra("whatever", whatever);
        final PendingIntent pendingIntent = PendingIntent.getActivities(ctx, UNIQUE_REQUEST_CODE++,
                new Intent[] {backIntent, intent}, PendingIntent.FLAG_ONE_SHOT);
    

    Just tested it. It works like a charm. I initially suspected that there might be a problem if the app is already running, and you go to a notification. I.e. say you activity stack is (topmost on the right):

    [MainActivity] [SomeActivity]
    

    and you click a notification for ConversationActivity. Would you get:?

    [MainActivity] [SomeActivity] [MainActivity] [ConversationActivity]
    

    Well, it turns out you magically get

    [MainActivity] [SomeActivity] [ConversationActivity]
    

    which is what I wanted, but I have no idea how it does that. I haven't set any special options on any of the activities. Oh well!

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

    There is a new guide on how to do this, and it suggest some slightly different practices than the answers above:

    https://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

    Note, don't actually use 0 for your requestCode in getPendingIntent. For me any number not 0 worked, but 0 resulted in the back button just going to the launcher.

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

    I know 2 ways of achieving this:

    1. Start your home activity from within activity started by notification. This may become quite trick, as you will have to differentiate and keep track of various possible entry points.
    2. You always start with you home activity which checks if is being started by a notification and then starts the notification activity. This ensures a consistent entry point and the back stack will be there when user press Up key.

    Regards.

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

    This solution works great exactly for this problem

    android:noHistory="true"
    
    0 讨论(0)
  • 2020-11-30 05:17

    Nice.

    But be aware that is does not work on pre-3.0 (pre API level 11) system because PendingIntent.getActivities() is not available there.

    And that is the same for constructing a backstack to access an activity from a notification (via Intent) and have the back button behaves coherently with your application (detail activity comes back to list activity for example).

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

    Activities are associated with a task which is nothing but a group of activities in the back stack. When you open up a new activity upon click of the notification, that activity is opened as an independent activity and is put into a new task. This means only the notification activity and the parent activity are present.

    Just specify the launch mode of your activity as "standard" in the manifest file and it should resolve the issue.

    Intent resultIntent = new Intent(getApplicationContext(),
                            ResultActivity.class);
    
    PendingIntent resultPendingIntent =
        PendingIntent.getActivity(getApplicationContext(),0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    
    mBuilder.setContentIntent(resultPendingIntent); 
    
    NotificationManager mNotificationManager = (NotificationManager)
        getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notId, mBuilder.build());
    

    manifest:

    <activity
                android:name="com.mns.exercisenotifications.ResultActivity"
                android:label="@string/app_name"
                android:launchMode="standard" >  <---
                <intent-filter>
                    <action android:name="com.mns.exercisenotifications.ResultActivity" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    
     </activity>
    
    0 讨论(0)
提交回复
热议问题