onNewIntent()生命周期和注册监听器
我正在使用oneTop Activity通过onNewIntent()从search对话框接收意图。
我注意到onPause()在onNewIntent()之前onNewIntent() ,然后调用onResume() 。 视觉:
- search对话框启动
- search意图激发到活动
-
onPause() -
onNewIntent() -
onResume()
问题是我在onResume()中注册了监听器,在onPause()中被移除,但是在onNewIntent()调用中需要它们。 有没有一个标准的方法来使这些听众可用?
onNewIntent()意味着已经在堆栈中其他地方运行的singleTop活动的入口点,因此不能调用onCreate() 。 从活动生命周期的angular度来看,因此需要在onNewIntent()之前调用onPause() onNewIntent() 。 我build议你重写你的活动,不要在onNewIntent()使用这些监听器。 例如,大部分时间我的onNewIntent()方法看起来像这样:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // getIntent() should always return the most recent setIntent(intent); }
通过使用getIntent()在onResume()发生所有设置逻辑。
除了第一次创build活动时,OnNewIntent()始终会被调用singleTop / Task活动。 当时在创造被称为。
你总是可以通过将onNewIntent方法放入onCreate方法来调用
@Override public void onCreate(Bundle savedState) { super.onCreate(savedState); onNewIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); //code }