Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Build a notification, TaskStackBuilder.addParentStack not working

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);
like image 841
David Fortunato Avatar asked Nov 29 '12 18:11

David Fortunato


4 Answers

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>
like image 113
Ralgha Avatar answered Nov 04 '22 00:11

Ralgha


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!

like image 44
penduDev Avatar answered Nov 04 '22 02:11

penduDev


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

like image 15
Rafael Avatar answered Nov 04 '22 01:11

Rafael


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)));
    }
    ...
}
like image 8
Italo Borssatto Avatar answered Nov 04 '22 00:11

Italo Borssatto