我正在使用singleTop活动来通过接收来自搜索对话框的意图onNewIntent()。
我注意到的是onPause()在调用之前onNewIntent(),然后调用它onResume()。视觉上:
- 搜索对话框启动
- 向活动触发搜索意图
onPause()onNewIntent()onResume()
问题是我在中注册了侦听器,但onResume()已将其删除onPause(),但在onNewIntent()通话中需要它们。有没有使这些监听器可用的标准方法?
我正在使用singleTop活动来通过接收来自搜索对话框的意图onNewIntent()。
我注意到的是onPause()在调用之前onNewIntent(),然后调用它onResume()。视觉上:
onPause()onNewIntent()onResume()问题是我在中注册了侦听器,但onResume()已将其删除onPause(),但在onNewIntent()通话中需要它们。有没有使这些监听器可用的标准方法?
Answers:
onNewIntent()是作为singleTop活动的入口点,这些活动已经在堆栈的其他地方运行,因此不能调用onCreate()。从生命周期的角度来看活动它因此,需要调用onPause()之前onNewIntent()。建议您重写活动,以免在中使用这些侦听器onNewIntent()。例如,大多数时候我的onNewIntent()方法看起来像这样:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
所有安装逻辑中发生的onResume()利用getIntent()。
Intent到onResume(),你的活动可能会尝试执行每次恢复的时间搜索,可能不是你想要的行为。
注意:从另一个方法调用生命周期方法不是一个好习惯。在下面的示例中,我试图实现无论您的Activity类型如何,始终都会调用onNewIntent。
OnNewIntent()总是被调用为singleTop / Task活动,但第一次创建活动时除外。那时,调用onCreate来提供解决方案,以解决此线程上询问的几个问题。
您可以始终通过将onNewIntent放入onCreate方法中来调用它,例如
@Override
public void onCreate(Bundle savedState){
super.onCreate(savedState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//code
}