Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...