onNewIntent()的生命周期和注册的侦听器 [英] onNewIntent() lifecycle and registered listeners

查看:210
本文介绍了onNewIntent()的生命周期和注册的侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是singleTop活动通过<一个接收来自一个搜索对话框意图href="http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29"><$c$c>onNewIntent().

I'm using a singleTop Activity to receive intents from a search-dialog via onNewIntent().

我注意到的是,的onPause()之前 onNewIntent称为(),然后再把它调用 onResume()。目测:

What I noticed is that onPause() is called before onNewIntent(), and then afterwards it calls onResume(). Visually:

  • 在搜索对话框启动
  • 被炒到了活动的搜索意图
  • 的onPause()
  • onNewIntent()
  • onResume()
  • search dialog initiated
  • search intent fired to activity
  • onPause()
  • onNewIntent()
  • onResume()

现在的问题是,我在 onResume注册的侦听器()的onPause得到删除(),但他们所需要的 onNewIntent()呼叫内。有没有一种标准的方式来使这些听众提供?

The problem is that I have listeners registered in onResume() that get removed in onPause(), but they are needed inside of the onNewIntent() call. Is there a standard way to make those listeners available?

推荐答案

onNewIntent()是指为切入点singleTop它已经在堆栈上运行其他地方活动,因此不能称之为的onCreate()。从观活动的生命周期角度它因此,需要调用的onPause() onNewIntent()。我建议你​​重写你的活动不使用这些监听器里面的 onNewIntent()。例如大部分的时间我 onNewIntent()方法只是看起来是这样的:

onNewIntent() is meant as entry point for singleTop activities which already run somewhere else in the stack and therefore can't call onCreate(). From activities lifecycle point of view it's therefore needed to call onPause() before onNewIntent(). I suggest you to rewrite your activity to not use these listeners inside of onNewIntent(). For example most of the time my onNewIntent() methods simply looks like this:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // getIntent() should always return the most recent
    setIntent(intent);
}

随着 onResume所有安装逻辑发生()利用 getIntent()

这篇关于onNewIntent()的生命周期和注册的侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆