Android - Build a notification, TaskStackBuilder.addParentStack not working

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I'm trying to launch an activity from a notification like the Android docs explain, but when I open the notification and then press the back button, the HomeActivity (parent) doesn't open, instead the application closes. What am I doing wrong?

    Intent resultIntent = new Intent(context, MatchActivity.class);;     resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);      // Adds the back stack for the Intent (but not the Intent itself)     stackBuilder.addParentStack(MainActivity.class);      stackBuilder.addNextIntent(resultIntent); 

回答1:

You need to add the parent stack for the activity you're launching, not the parent of it.

Replace:

stackBuilder.addParentStack(MainActivity.class); 

with:

stackBuilder.addParentStack( MatchActivity.class ); 

This assumes that you've defined the parent in your Manifest (API 16+):

If you're developing for under API 16, then you have to define the parent as:



回答2:

If none of the solutions are working and you are sure that you have followed everything carefully...then you need you uninstall the app and reinstall it. Worked for me!



回答3:

Using TaskStackBuilder didn't solve my problem and works only for Honeycomb and greater. So I take the following solution (please, don't crucify me):

  1. Call MainActivity instead of MatchActivity, passing MatchActivity as argument (by Intent).
  2. In MainActivity.onCreate, start the MatchActivity if the parameter is available.

New code:

Intent resultIntent = new Intent(context, MainActivity.class) //         .putExtra(MainActivity.ACTIVITY_EXTRA, MatchActivity.class.getName()) //         .putExtra("Pass extras to MatchActivity", "if you want! :)");  PendingIntent pendingIntent = PendingIntent.getActivity(context, visitId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);  Notification notification = new NotificationCompat.Builder(context) //             .setContentIntent(pendingIntent) //             .build(); 

On MainActivity:

public static final String ACTIVITY_EXTRA = "activity";  @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     if (getIntent().getStringExtra(ACTIVITY_EXTRA) != null) {         startActivity(new Intent(getIntent()).setClassName(this, getIntent().getStringExtra(ACTIVITY_EXTRA)));     }     ... } 


回答4:

Intent resultIntent = new Intent(App.getContext(), TargetActivity.class); Intent backIntent = new Intent(App.getContext(), ParentActivity.class); backIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); final PendingIntent resultPendingIntent = PendingIntent.getActivities(                                     App.getContext(), 0,                 new Intent[]{backIntent, resultIntent}, PendingIntent.FLAG_ONE_SHOT); mNotifyBuilder.setContentIntent(resultPendingIntent); 

this solved my problem with Parent stack on Notification Click



回答5:

Have you looked in the Android documentation, specifically the Notifications API guide. It describes how to do this in detail.

Notice that if the Activity you start from the notification is not part of the normal Activity flow, then it should not go to the start page of the app; instead, it should go to the Home screen.



回答6:

As stated in other answers, TaskStackBuilder doesn't work for versions below Honeycomb.

My solution was to override the activity's onBackPressed() method.

@Override public void onBackPressed() {     NavUtils.navigateUpFromSameTask(this); } 

Obviously if you're planning on finishing the activity in some other manner you will have to handle that as well. (Though I imagine overriding finish() will have some unexpected behaviour).



回答7:

You should add this to the MainActivity on the AndroidManifest:



回答8:

For me the stackBuilder.addParentStack didn't work.

I end up doing this, hope this could helps you.

    Intent intent = new Intent(context, MatchActivity.class);      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);     // Adds the back stack for the Intent     stackBuilder.addNextIntentWithParentStack(new Intent(context, MainActivity.class));     stackBuilder.addNextIntent(intent);     PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 


回答9:

I had the same problem! Solve:

switch to

PendingIntent resultPendingIntent =   PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);  PendingIntent resultPendingIntent =   stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 


回答10:

If you use code generation in your project (like Dagger) the MainActivity.class should be replaced with the MainActivity_.class (or whatever your parent activity name is). Took me a whole day to figure this out. Hope this can save someone's day.



回答11:

also this can happen if your activiti in mannifest has next launchMode:



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!