NavUtils.shouldU precreateTask的软糖失败 [英] NavUtils.shouldUpRecreateTask fails on JellyBean

查看:314
本文介绍了NavUtils.shouldU precreateTask的软糖失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发出的通知所选择的时候开始活动的应用程序。根据Android的文档,我可以使用NavUtils.shouldU precreateTask检查是否该活动已直接启动(即从通知)或通过正常活动堆栈。然而,它给出了错误的答案。我的软糖测试这一点,但使用的支持库。

I have an application that issues notifications that when selected start an activity. According to the Android docs I can use NavUtils.shouldUpRecreateTask to check whether the activity has been started directly (i.e. from the notification) or via a normal activity stack. However it gives the wrong answer. I'm testing this on JellyBean but using the support library.

基本上shouldU precreateTask总是返回假,甚至当活动已开始形成所述通知。

Basically shouldUpRecreateTask always returns false, even when the activity has been started form the notification.

为什么shouldU precreateTask未能给出正确答案任何想法?

Any ideas on why shouldUpRecreateTask is failing to give the correct answer?

推荐答案

我仍然不知道为什么shouldU precreateTask失败 - 查看源$ C ​​$ C因为它没有太大的帮助。但是该解决方案是相当简单的 - 我只是一个额外的标志值添加到被附加到通知的意图,并检查这个中的onCreate()。如果它被设置,则该活动已被从通知调用,所以后面堆具有重新创建。

I still don't know why shouldUpRecreateTask fails - looking at the source code for it doesn't help much. But the solution is quite simple - I just add an extra flag value to the Intent that is attached to the notification, and check this in onCreate(). If it is set, then the Activity has been invoked from the notification, so the back stack has to be recreated.

在code是这样的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle b = getIntent().getExtras();
    fromNotification = b.getInt("fromNotification") == 1;
    setContentView(R.layout.threadlist);
}

@Override
public boolean onHomeButtonPressed() {
    if(fromNotification) {
        // This activity is not part of the application's task, so create a new task
        // with a synthesized back stack.
        TaskStackBuilder tsb = TaskStackBuilder.from(this)
                .addNextIntent(new Intent(this, COPAme.class));
        tsb.startActivities();
    } 
        // Otherwise, This activity is part of the application's task, so simply
        // navigate up to the hierarchical parent activity.
    finish();
    return true;
}

这篇关于NavUtils.shouldU precreateTask的软糖失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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