Up to parent activity - on Android

后端 未结 7 1557
有刺的猬
有刺的猬 2021-01-17 13:06

After receiving a notification in my app, clicking on it opens activity B. Activity B has a parent activity A. Here is the manifest:



        
7条回答
  •  不思量自难忘°
    2021-01-17 13:18

    If @Manish Mulimani's solution worked for you, then please feel free to ignore this information. It didn't work for me, and I haven't tried the code that is in OP, but here is an alternate solution if neither of those worked for you:

    First, as OP did, specify the parent Activity in AndroidManifest.xml:

    
    
        
        
    
    

    Then, in the code you write to show your notification, do the following:

    /* The Intent you want to open when your notification is clicked */
    Intent detailIntent = new Intent(context, DetailActivity.class);
    /* If you have some extras or a URI, set it here for the DetailActivity */
    detailIntent.setData(todaysWeatherUri);
    
    /*
     * This Intent is the one that goes back to your MainActivity when the up 
     * arrow or back button is clicked from the DetailActivity. 
     */
    Intent backToMain = new Intent(context, MainActivity.class);
    
    PendingIntent resultPendingIntent =
            TaskStackBuilder.create(context)
                    .addNextIntentWithParentStack(backToMain)
                    .addNextIntent(detailIntent)
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);   
    

    I've tested this with every iteration of starting Activities that I can thin of and it works perfectly! I hope it helps you.

    Here is the link for the Android Documentation from where I formed this solution.

提交回复
热议问题