错误:onNewIntent不要求与Intent.FLAG_ACTIVITY_NEW_TASK singleTop活动 [英] Bug: onNewIntent not called for singleTop activity with Intent.FLAG_ACTIVITY_NEW_TASK

查看:287
本文介绍了错误:onNewIntent不要求与Intent.FLAG_ACTIVITY_NEW_TASK singleTop活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有一个错误 startActivity

通过制定活动是 singleTop 在AndroidManifest.xml中,并使用 taskAffinity 不同> Intent.FLAG_ACTIVITY_NEW_TASK startActivity ,两项活动可以在两个任务(每个任务一个活动)来创建。

调用 startActivity 再次将返回到第一个活动/任务和 onNewIntent 被调用。但是,调用 startActivity A第四次将返回第二个活动/任务,但 onNewIntent 不叫。

这两个任务之间的唯一区别是它们的 taskAffinity 。不知怎的,不对称的行为是观察。

但是,如果 Intent.FLAG_ACTIVITY_SINGLE_TOP 也被使用,那么 onNewIntent 被称为预期。

这样看来, singleTop 在AndroidManifest.xml中不在<$一样 Intent.FLAG_ACTIVITY_SINGLE_TOP C $ C>意图。

公共类ActivityA扩展活动实现OnClickListener {
    私人字符串标签;

    @覆盖
    公共无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);

        标签=的getClass()的getName()。
        Log.v(标签的onCreate());

        的setContentView(R.layout.main);
        Button按钮=(按钮)findViewById(R.id.button);
        button.setText(tag.endsWith(ActivityA)?b活动
                活动A);
        button.setOnClickListener(本);
    }

    @覆盖
    公共无效的onClick(视图v){
        意向意图;
        INT标志= Intent.FLAG_ACTIVITY_NEW_TASK
        // | Intent.FLAG_ACTIVITY_SINGLE_TOP
        ;

        Log.v(标签的onClick());

        意图=新的意图(这一点,
                tag.endsWith(ActivityA)? ActivityB.class
                        :ActivityA.class);
        intent.setFlags(标志);
        startActivity(意向);
    }

    @覆盖
    保护无效onNewIntent(意向意图){
        Log.v(标签,onNewIntent());
    }
}

公共类ActivityB扩展ActivityA {

}

&LT;?XML版本=1.0编码=UTF-8?>
&LT;舱单
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.test
    安卓版code =1
    机器人:VERSIONNAME =1.0>
    &lt;应用
        机器人:图标=@可绘制/图标
        机器人:标签=@字符串/ APP_NAME>

        &LT;活动
            机器人:名称=。ActivityA
            机器人:launchMode =singleTop
            机器人:标签=活动A>
            &LT;意图过滤器>
                &lt;作用
                    机器人:名称=android.intent.action.MAIN/>
                &LT;类别
                    机器人:名称=android.intent.category.LAUNCHER/>
            &LT; /意图过滤器>
        &LT; /活动>

        &LT;活动
            机器人:名称=。ActivityB
            机器人:launchMode =singleTop
            机器人:标签=b活动
            机器人:taskAffinity =activity.B>
        &LT; /活动>

    &LT; /应用程序>
&LT; /清单>

解决方案

这是一个有点晚了,但因为我只是碰到这个 - 马特提到,在清单和意图旗活动单顶是不一样的(至少在Android上的previous版本)。

startActivityUncheckedLocked 方法:

在姜饼 - <一href="https://github.com/android/platform_frameworks_base/blob/gingerbread/services/java/com/android/server/am/ActivityStack.java">https://github.com/android/platform_frameworks_base/blob/gingerbread/services/java/com/android/server/am/ActivityStack.java

看行2204 -

  IF((launchFlags&安培;!Intent.FLAG_ACTIVITY_SINGLE_TOP)= 0
                                &功放;&安培; taskTop.realActivity.equals(r.realActivity)){
 

所以它只是检查的目的推出的标志,而不是检查清单。比较这对软糖MR1版本:

<一个href="https://github.com/android/platform_frameworks_base/blob/jb-mr1-release/services/java/com/android/server/am/ActivityStack.java">https://github.com/android/platform_frameworks_base/blob/jb-mr1-release/services/java/com/android/server/am/ActivityStack.java

看行2835 -

 如果(((launchFlags&安培;!Intent.FLAG_ACTIVITY_SINGLE_TOP)= 0
                        || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
                        &功放;&安培; taskTop.realActivity.equals(r.realActivity)){
 

如此看来,在MR1,他们除了检查r.launchMode(presumably的的Andr​​oidManifest.xml 值) launchFlags (presumably的意图本身),而在旧版本中,他们只检​​查目的的标志。

它实际上是固定在2012年2月16日,在此承诺: <一href="https://github.com/android/platform_frameworks_base/commit/f363dfd26c304bca33f12065a9ed3de291193962">https://github.com/android/platform_frameworks_base/commit/f363dfd26c304bca33f12065a9ed3de291193962

短的版本是 - 设置单个顶级标志双方的意图,并在清单

(感谢在android的bug跟踪系统相关的错误此评论指着我看在哪个源文件 - <一个href="http://$c$c.google.com/p/android/issues/detail?id=4155#c9">http://$c$c.google.com/p/android/issues/detail?id=4155#c9).

There appears to be a bug in startActivity.

By setting activities to be singleTop with different taskAffinity in AndroidManifest.xml and using the Intent.FLAG_ACTIVITY_NEW_TASK when calling startActivity, two activities can be created in two tasks (one activity per task).

Calling startActivity again will return to the first activity/task and onNewIntent is called. However, calling startActivity a forth time will return to the second activity/task, but onNewIntent is not called.

The only difference between the two tasks is their taskAffinity. Somehow, asymmetrical behaviour is observed.

However, if the Intent.FLAG_ACTIVITY_SINGLE_TOP is also used, then onNewIntent is called as expected.

It would appear that singleTop in AndroidManifest.xml is not the same as Intent.FLAG_ACTIVITY_SINGLE_TOP in the Intent.

public class ActivityA extends Activity implements OnClickListener {
    private String tag;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tag = getClass().getName();
        Log.v(tag, "onCreate()");

        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button);
        button.setText(tag.endsWith("ActivityA") ? "Activity B"
                : "Activity A");
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        int flags = Intent.FLAG_ACTIVITY_NEW_TASK
        // | Intent.FLAG_ACTIVITY_SINGLE_TOP
        ;

        Log.v(tag, "onClick()");

        intent = new Intent(this,
                tag.endsWith("ActivityA") ? ActivityB.class
                        : ActivityA.class);
        intent.setFlags(flags);
        startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.v(tag, "onNewIntent()");
    }
}

public class ActivityB extends ActivityA {

}

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test"
    android:versionCode="1"
    android:versionName="1.0">
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">

        <activity
            android:name=".ActivityA"
            android:launchMode="singleTop"
            android:label="Activity A">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN" />
                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ActivityB"
            android:launchMode="singleTop"
            android:label="Activity B"
            android:taskAffinity="activity.B">
        </activity>

    </application>
</manifest> 

解决方案

this is a little bit late, but since i just came across this - as Matt mentions, the flag activity single top in the manifest and in the intent are not the same (at least on previous versions of android).

in startActivityUncheckedLocked method:

on gingerbread - https://github.com/android/platform_frameworks_base/blob/gingerbread/services/java/com/android/server/am/ActivityStack.java

look at line 2204 --

 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
                                && taskTop.realActivity.equals(r.realActivity)) {

so it's just checking the launch flags on the intent, and not checking the manifest. compare this to jellybean mr1 release:

https://github.com/android/platform_frameworks_base/blob/jb-mr1-release/services/java/com/android/server/am/ActivityStack.java

look at line 2835 --

                if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
                        || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
                        && taskTop.realActivity.equals(r.realActivity)) {

so it seems that in mr1, they are checking r.launchMode (presumably the AndroidManifest.xml value) in addition to the launchFlags (presumably of the intent itself), whereas in the older versions, they were only checking the intent flags.

it was actually fixed on february 16th, 2012, in this commit: https://github.com/android/platform_frameworks_base/commit/f363dfd26c304bca33f12065a9ed3de291193962

short version is - set the single top flag in both the intent and in the manifest.

(thanks to this comment on the associated bug in the android bug tracker for pointing me to which source file to look in - http://code.google.com/p/android/issues/detail?id=4155#c9).

这篇关于错误:onNewIntent不要求与Intent.FLAG_ACTIVITY_NEW_TASK singleTop活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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