安卓“单顶"启动模式和 onNewIntent 方法 [英] Android "single top" launch mode and onNewIntent method
问题描述
我在 Android 文档中读到,通过将我的 Activity 的 launchMode 属性设置为 singleTop 或通过将 FLAG_ACTIVITY_SINGLE_TOP
标志添加到我的 Intent,调用 startActivity(intent)
将重用单个 Activity 实例并在 onNewIntent
回调中给我 Intent.我做了这两件事,onNewIntent
从不触发,onCreate
每次都触发.文档还说 this.getIntent()
返回首次创建时首先传递给 Activity 的意图.在 onCreate
中,我正在调用 getIntent
并且每次都会得到一个新对象(我在另一个活动中创建了意图对象并为其添加了一个额外的对象...这个额外的东西每次都应该是一样的,如果它返回给我相同的意图对象).这一切让我相信我的活动不像单顶",我不明白为什么.
I read in the Android documentation that by setting my Activity's launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP
flag to my Intent, that calling startActivity(intent)
would reuse a single Activity instance and give me the Intent in the onNewIntent
callback. I did both of these things, and onNewIntent
never fires and onCreate
fires every time. The docs also say that this.getIntent()
returns the intent that was first passed to the Activity when it was first created. In onCreate
I'm calling getIntent
and I'm getting a new one every time (I'm creating the intent object in another activity and adding an extra to it...this extra should be the same every time if it was returning me the same intent object). All this leads me to believe that my activity is not acting like a "single top", and I don't understand why.
如果我只是遗漏了一个必需的步骤,要添加一些背景,这是我在清单中的 Activity 声明和我用来启动 Activity 的代码.活动本身在这方面没有做任何值得一提的事情:
To add some background in case I'm simply missing a required step, here's my Activity declaration in the manifest and the code I'm using to launch the activity. The Activity itself doesn't do anything worth mentioning in regards to this:
在 AndroidManifest.xml 中:
in AndroidManifest.xml:
<activity
android:name=".ArtistActivity"
android:label="Artist"
android:launchMode="singleTop">
</activity>
在我的通话活动中:
Intent i = new Intent();
i.putExtra(EXTRA_KEY_ARTIST, id);
i.setClass(this, ArtistActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
推荐答案
你是否检查了 onDestroy()
是否也被调用了?这可能就是为什么每次都调用 onCreate()
而不是 onNewIntent()
的原因,只有当活动已经存在时才会调用.
Did you check if onDestroy()
was called as well? That's probably why onCreate()
gets invoked every time instead of onNewIntent()
, which would only be called if the activity is already existing.
例如,如果您通过后退按钮离开您的活动,默认情况下它会被销毁.但是,如果您在活动堆栈的更高层进入其他活动,并从那里再次调用您的 ArtistActivity.class
,它将跳过 onCreate()
并直接转到 onNewIntent()
,因为 Activity 已经被创建,并且因为你将它定义为 singleTop
Android 不会创建它的新实例,而是使用已经存在的实例.
For example if you leave your activity via the BACK-button it gets destroyed by default. But if you go up higher on the activity stack into other activities and from there call your ArtistActivity.class
again it will skip onCreate()
and go directly to onNewIntent()
, because the activity has already been created and since you defined it as singleTop
Android won't create a new instance of it, but take the one that is already lying around.
我做什么来看看发生了什么 我为每个活动的所有不同状态实现了虚拟函数,所以我总是现在正在发生什么:
What I do to see what's going on I implement dummy functions for all the different states of each activity so I always now what's going on:
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy()");
super.onDestroy();
}
同onRestart()
、onStart()
、onResume()
、onPause()
、<代码>onDestroy()
如果上述(后退按钮)不是您的问题,那么实现这些假人至少可以帮助您更好地调试它.
If the above (BACK-button) wasn't your problem, implementing these dummies will at least help you debugging it a bit better.
这篇关于安卓“单顶"启动模式和 onNewIntent 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!