Android的 - 建立一个通知,TaskStackBuilder.addParentStack不工作 [英] Android - Build a notification, TaskStackBuilder.addParentStack not working

查看:965
本文介绍了Android的 - 建立一个通知,TaskStackBuilder.addParentStack不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动从通知的活动,如Android的文档说明,但是当我打开通知,然后preSS后退按钮时,HomeActivity(父母)不开,而不是应用程序关闭。我究竟做错了什么?

 意图resultIntent =新的意图(背景下,MatchActivity.class);;
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(上下文);

    //添加背部栈的意图(而不是意图本身)
    stackBuilder.addParentStack(MainActivity.class);

    stackBuilder.addNextIntent(resultIntent);
 

解决方案

您需要添加父堆栈为你推出了它,而不是父母的活动。

替换:

  stackBuilder.addParentStack(MainActivity.class);
 

  stackBuilder.addParentStack(MatchActivity.class);
 

这假定您已经定义了父在清单(API 16 +):

 <活动机器人:名称=。MatchActivity
    机器人:parentActivityName =。MainActivity
    ... />
 

如果你在16 API开发,那么你必须定义父为:

 <活动机器人:名称=。MatchActivity>
    &所述;元数据
        机器人:名称=android.support.PARENT_ACTIVITY
        MainActivity:机器人值= />
< /活性GT;
 

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

解决方案

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>

这篇关于Android的 - 建立一个通知,TaskStackBuilder.addParentStack不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆