简体   繁体   English

onNewIntent()生命周期和已注册的侦听器

[英]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: 视觉:

  • search dialog initiated 搜索对话框已启动
  • search intent fired to activity 搜索意图触发了活动
  • 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.

相关问题 有关活动生命周期的问题:onNewIntent / onSaveInstanceState - Question about activity lifecycle: onNewIntent / onSaveInstanceState BroadcastReceiver(清单已注册)生命周期 - BroadcastReceiver (Manifest registered) lifecycle 使用onNewIntent和活动生命周期来验证是否已看到消息 - Using onNewIntent and activity lifecycle to verify if a message has been seen 服务的生命周期以及取消注册监听器的最佳方式 - Lifecycle of a Service and the best way to unregister Listeners Flutter 插件 - 如何注销生命周期监听器? - Flutter plugin - How to deregister Lifecycle listeners? 哪个生命周期事件最适合注册/注销监听器? - Which lifecycle event is best to register/unregister listeners? 打ze模式如何影响注册的侦听器(尤其是传感器侦听器) - How does Doze mode affect registered listeners (especially sensor listeners) 为什么我的所有听众都没有注册? - Why aren't all my listeners registered? Android BroadcastReceiver生命周期:动态注册接收器的文档错误? - Android BroadcastReceiver Lifecycle: documentation wrong for dynamically registered receiver? 当多个听众注册时,加速计数据与陀螺仪混合 - Accelerometer data is mixed with Gyroscope when multiple listeners are registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM