Android:获取最新意图 [英] Android: get most recent intent
问题描述
如何获取发送给活动的最后意图?
How can I get the last intent sent to an activity?
onNewIntent()
的文档建议我需要执行以下操作:
The docs for onNewIntent()
suggest I would need to do something like this:
class MyActivity {
public void onNewIntent(Intent intent){
setIntent(intent);
reactToIntentAndDoStuff()
super.onNewIntent(Intent intent);
}
public void onCreate(){
reactToIntentAndDoStuff()
super.onCreate();
}
public void onResume(){
reactToIntentAndDoStuff()
super.onResume();
}
public void reactToIntentAndDoStuff(){
Intent intent = getIntent();
}
}
这看起来正确吗?或者,还有更好的方法?我的活动的launchMode
将是singleTop
.无论是否已实例化,它都将需要以大致相同的方式对相同的Intent做出反应.
Does this look about right? Or is there a better way? My activity's launchMode
will be singleTop
. It will need to react to the same Intent in more or less the same way, whether it's already instantiated or not.
推荐答案
我认为这是正确的方法:) 请检查您是否真的需要在onResume()方法中调用reactToIntentAndDoStuff().我认为不需要.
I think this is the correct way :) Please check if you really need to call reactToIntentAndDoStuff() in onResume() method. I think its not needed.
super.onCreate();应该是onCreate()方法的第一行.
super.onCreate(); should be first line in onCreate() method.
getIntent()返回一个Intent,它是Activity首次启动时收到的.除非我们调用setIntent(newIntent)方法,否则不会更新.
getIntent() returns an Intent which was received when Activity launched first time. This does not update unless we call setIntent(newIntent) method.
这篇关于Android:获取最新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!