[英]onNewIntent() lifecycle and registered listeners
I'm using a singleTop Activity to receive intents from a search-dialog via onNewIntent()
. 我正在使用singleTop Activity通过onNewIntent()
从搜索对话框接收意图。
What I noticed is that onPause()
is called before onNewIntent()
, and then afterwards it calls onResume()
. 我注意到在onNewIntent()
之前调用onPause()
,然后调用onResume()
。 Visually: 视觉:
onPause()
onNewIntent()
onResume()
The problem is that I have listeners registered in onResume()
that get removed in onPause()
, but they are needed inside of the onNewIntent()
call. 问题是我在onResume()
中注册了在onPause()
中被删除的侦听器,但是在onNewIntent()
调用中需要它们。 Is there a standard way to make those listeners available? 有没有一种标准方法可以让这些听众可用?
onNewIntent()
is meant as entry point for singleTop activities which already run somewhere else in the stack and therefore can't call onCreate()
. onNewIntent()
意味着singleTop活动的入口点已经在堆栈中的其他位置运行,因此无法调用onCreate()
。 From activities lifecycle point of view it's therefore needed to call onPause()
before onNewIntent()
. 从活动生命周期的角度来看,因此需要在onNewIntent()
之前调用onPause()
onNewIntent()
。 I suggest you to rewrite your activity to not use these listeners inside of onNewIntent()
. 我建议你重写你的活动,不要在onNewIntent()
使用这些监听器。 For example most of the time my onNewIntent()
methods simply looks like this: 例如,大多数时候我的onNewIntent()
方法看起来像这样:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
With all setup logic happening in onResume()
by utilizing getIntent()
. 使用getIntent()
在onResume()
发生所有设置逻辑。
Note: Calling a lifecycle method from another one is not a good practice. 注意:从另一个方法调用生命周期方法不是一个好习惯。 In below example I tried to achieve that your onNewIntent will be always called irrespective of your Activity type. 在下面的示例中,我试图实现无论您的Activity类型如何,都将始终调用onNewIntent。
OnNewIntent() always get called for singleTop/Task activities except for the first time when activity is created. 除了第一次创建活动时,OnNewIntent()总是被调用singleTop / Task活动。 At that time onCreate is called providing to solution for few queries asked on this thread. 那时onCreate被称为提供解决方案,在这个线程上询问的几个查询。
You can invoke onNewIntent always by putting it into onCreate method like 您可以通过将onNewIntent放入onCreate方法来调用onNewIntent
@Override
public void onCreate(Bundle savedState){
super.onCreate(savedState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.