62

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);
Khantahr
  • 8,156
  • 4
  • 37
  • 60
David Fortunato
  • 769
  • 1
  • 6
  • 19
  • 3
    For anyone wondering, the accepted answer works, but resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK) is not present in the Android docs. It gave me hell. – wsgeorge Dec 03 '15 at 12:43

11 Answers11

100

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+):

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

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

<activity android:name=".MatchActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>
Khantahr
  • 8,156
  • 4
  • 37
  • 60
  • It only works for API level 16 and higher. If you're below that then you have to use the meta tags in the Manifest. Sorry I have to run, can't get more detailed than that right now. – Khantahr Nov 29 '12 at 18:54
  • No problem, thanks anyway :) I did this: But still not working... – David Fortunato Nov 29 '12 at 19:09
  • 4
    On further investigation, it works on a stock 4.1.1 phone. Using AOKP 4.1.2, it doesn't work though. – staackuser2 Dec 01 '12 at 23:51
  • 4
    The TaskStackBuilder creates the correct back stack only for devices with Honeycomb(3.1) or greater. On Gingerbread(2.3) and earlier it uses only the top Intent to create the PendingIntent. – thaussma Dec 13 '12 at 15:44
  • Please try use full Activity class name instead of related. It work for me on Android 10 (Gingerbread). – rude Nov 03 '14 at 12:15
  • In my case I used absolute path in Manifest for ParentActivity. And it caused problems. When I switched to relative path .ParentClass.class BackStack started to work correctly. – Rafael Feb 05 '15 at 07:58
  • 8
    if it still doesn't work, uninstall and install again – Simon Jan 07 '17 at 02:30
  • Thanks. Worked for me. – Saeid Z Apr 20 '21 at 17:56
84

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!

penduDev
  • 4,743
  • 35
  • 37
  • 18
    Unbelievable... this was it for me! Thanks – Nick H Apr 19 '16 at 23:49
  • 6
    The times when you really don't know whether to be happy or mad it worked :) >:| – Jraco11 Aug 17 '16 at 00:46
  • 9
    It worked for me too and I'm not happy at all. What will happen to the users out there once we publish our app? it's not viable to uninstall and install the app in order to get this work. I don't understand anything... – GoRoS Aug 18 '16 at 13:57
  • 1
    It is because of instant build, I suppose. – mihirjoshi Sep 13 '16 at 11:44
  • 4
    I got this problem even without instant run. First I tried clearing data and it made no effect. You *do* need to uninstall the app. Just guessing here but it might be that adding parentActivity can't be changed by an update. – Pedro Loureiro Dec 06 '16 at 06:31
  • I was trying to get it right for one hour straight and had no clue as to what I did wrong. Then I uninstalled and installed and it worked! Thanks! – Dawied Dec 06 '16 at 16:37
  • 1
    @Asimov haha! I was clueless for about 4 hours before I accidentally installed it on my other phone. Glad it helped! – penduDev Dec 06 '16 at 20:27
  • 8
    Is this a documented bug in Android?? – hooby3dfx Feb 20 '17 at 15:14
15
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

Rafael
  • 6,091
  • 5
  • 54
  • 79
8

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)));
    }
    ...
}
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
4

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);
Pablo
  • 1,368
  • 12
  • 18
2

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.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
2

You should add this to the MainActivity on the AndroidManifest:

<activity
   android:name=".MainActivity"
   android:allowTaskReparenting="true" />
Rafa0809
  • 1,733
  • 21
  • 24
1

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).

kassim
  • 3,880
  • 3
  • 26
  • 27
0

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);
Amit Singh
  • 2,267
  • 4
  • 25
  • 50
0

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.

Mykola
  • 435
  • 4
  • 17
0

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

<activity android:name=".SecondActivity"
     ...
         android:launchMode="singleInstance"
         android:parentActivityName=".MainActiviy"
    ...
/>
Andriy Antonov
  • 1,360
  • 2
  • 15
  • 29