Android - create notification, TaskStackBuilder.addParentStack not working

I'm trying to start an event from a notification, as Android docs explain, but when I open the notification and then click the back button, the HomeActivity (parent) does not 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); 
+46
android notifications
Nov 29 '12 at 18:39
source share
11 answers

You need to add the parent stack for the triggering action, not its parent.

Replace:

 stackBuilder.addParentStack(MainActivity.class); 

from:

 stackBuilder.addParentStack( MatchActivity.class ); 

It is assumed that you have defined the parent in the manifest (API 16 +):

 <activity android:name=".MatchActivity" android:parentActivityName=".MainActivity" ... /> 

If you are developing for API 16, then you need to define the parent as:

 <activity android:name=".MatchActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> 
+86
Nov 29 '12 at 18:45
source share
โ€” -

If none of the solutions work, and you are sure that you carefully watched it ... then you need to uninstall the application and reinstall it. Worked for me!

+46
Mar 20 '16 at 5:59
source share

Using TaskStackBuilder did not solve my problem and only works for Honeycomb and more. Therefore, I make the following decision (please do not crucify me):

  • Call MainActivity instead of MatchActivity , passing MatchActivity as an argument (by Intent).
  • In MainActivity.onCreate run MatchActivity , if 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))); } ... } 
+7
Nov 28 '13 at 19:14
source share
 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 parent stack issue in Notification Click

+5
Oct 19 '15 at 9:25
source share

Have you looked at the documentation on Android, in particular Notifications API Guide. It describes how to do this in detail.

Please note that if the action that you start with the notification is not part of the normal activity flow, it should not go to the applicationโ€™s start page; instead, it should go to the main screen.

+2
Nov 29 '12 at 21:05
source share

As indicated in other answers, TaskStackBuilder does not work for versions below Honeycomb.

My solution was to override the onBackPressed() method.

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

Obviously, if you plan to complete your activity in any other way, you will also have to process it. (Although I assume that overriding finish() will have some unexpected behavior).

+1
May 15 '14 at 9:55
source share

You should add this to MainActivity on AndroidManifest:

 <activity android:name=".MainActivity" android:allowTaskReparenting="true" /> 
+1
Nov 09 '15 at 1:26
source share

For me, stackBuilder.addParentStack does not work.

I end up doing this, hope this can help 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); 
+1
Jul 17 '16 at 16:59
source share

I had the same problem! Decision:

switch to

 PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
0
Sep 05 '14 at
source share

If you use code generation in your project (for example, Dagger), MainActivity.class should be replaced with MainActivity_.class (or regardless of the name of your parent action). Took me all day to figure this out. Hope this can save someone a day.

0
Feb 20 '17 at 12:20
source share

this can also happen if your activiti in mannifest has the following Mode launch :

 <activity android:name=".SecondActivity" ... android:launchMode="singleInstance" android:parentActivityName=".MainActiviy" ... /> 
0
Dec 20 '17 at 17:30
source share



All Articles