`
yhz61010
  • 浏览: 552310 次
  • 来自: -
博客专栏
63c13ecc-ef01-31cf-984e-de461c7dfde8
libgdx 游戏开发
浏览量:11999
社区版块
存档分类
最新评论

[原创] Android Activity onNewIntent() 详解

阅读更多
阅读难度:中
阅读前提
1. 需要了解 Android 的生命周期,每个方法的触发时机以及作用。
2. 需要了解 Activity 的 launchMode 模式和作用。
3. Intent 基本知识及作用。

Android Activity 的生命周期如下(图片来自 Android 官网 https://developer.android.com/guide/components/images/activity_lifecycle.png):

也就是说,初次启动 Activity 时,调用顺序如下:
onCreate() -> onStart() -> onResume()

那么,onNewIntent() 是什么时候被触发的呢,它又有什么用呢?
让我们再来看一张图:

上面这张图直观的说明了 onNewIntent() 是什么时候被调用的。而且其中还表明了最重要的一点:onCreate() 和 onNewIntent() 不会被同时调用。

接下来,我们再来看看官网是如何解释 onNewIntent() 的(https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)):
引用
onNewIntent

added in API level 1
void onNewIntent (Intent intent)
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Parameters
intent Intent: The new intent that was started for the activity.

简单说明下:如果在 AndroidManifest.xml 中,将 Activity 的 launchMode 设置成了 "singleTop" 模式,或者在调用 startActivity(Intent) 时,设置了
FLAG_ACTIVITY_SINGLE_TOP 标识,那么,当该 Activity 再次被启动时,如果它依然存在于 Activity 栈中,并且刚好处于栈的最顶层时,那么它将不会被重新创建,而是直接使用原来的实例,此时,onNewIntent(Intent) 将会被调用,后续生命周期中的其它方法,就可以使用 onNewIntent(Intent) 传递过来的新的 Intent 参数了。(也就是说,其它方法可以使用更新后的 Intent 参数)

也就是说,调用顺序如下:
onNewIntent() -> onRestart() -> onStart() -> onResume()

需要特别注意的是, 如果在 onNewIntent(Intent) 中,不调用 setIntent(Intent) 方法对 Intent 进行更新的话,那么之后在调用 getIntent() 方法时得到的依然是最初的值。

在实际编写代码过程中,我们往往需要在 onCreate() 和 onNewIntent() 中进行一些相同的处理,因此可以像下面这样做:
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_schedule);
     // Do what you want.
     onNewIntent(getIntent());
     // Do others
}
如果你不想直接在 onCreate() 方法中调用 onNewIntent(),那么也可以在 onCreate() 和 onNewIntent() 中同时调用一个自定义方法,也可以达到相同的目的:
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_schedule);
     // Do what you want.
     sameProcess(getIntent());
     // Do others
}

@Override
protected void onNewIntent(Intent intent) {
     // Do what you want.
     sameProcess(intent);
     // Do others
}

最后总结一下,在启动 Activtiy 时,如果希望在 onCreate() 不被触发的的情况下,依然可以对 Intent 进行操作,这就需要使用 onNewIntent()。

参考文献:
  • 大小: 80.7 KB
  • 大小: 90 KB
0
0
分享到:
评论

相关推荐

    Android onNewIntent()触发机制及注意事项

    在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent 1、其他应用发Intent,执行下列方法: onCreate onStart onResume 发Intent的方法: Uri uri = ...

    android onnewintent

    android onnewintent机制

    android中的Activity启动方式

    Android总Activity的启动模式分为四种: Activity启动模式设置: <activity android:name=".MainActivity"android:launchMode="standard" /> Activity的四种启动模式: 1.standard 模式启动模式,每次激活...

    onNewIntent

    android onNewIntent方法调用的问题跟launchmode有关

    activity的四种启动模式和onNewIntent的关系

    activity的四种启动模式和onNewIntent的关系

    Android中Activity的四种启动模式和onNewIntent()

    android 中activity的启动模式分为四种,(standard、singleTop、singTask、singleInstance),本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧

    onNewIntent方法的使用

    这个demo主要讲解了 onNewIntent() 方法的使用,希望可以帮助需要的同学.

    android 退出app 退出应用程序

    2 设置入口activity的启动模式android:launchMode="singleTask",重写onNewIntent方法, 在退出activity时,设置intent.setAction("exit_app");然后跳转到栈底activity, 这样就可以清除栈底之外的所有activity,...

    Android编程实现两个Activity相互切换而不使用onCreate()的方法

    本文实例讲述了Android编程实现两个Activity相互切换而不使用onCreate()的方法。分享给大家供大家参考,具体如下: 这里要实现的目的是: 有3个Activity: A,B,C,从A中可以进入B,B中可以进入C,并且B和C之间可能...

    android nfc 读写demo

    1.支持android设备读取nfc贴纸数据 2.支持向nfc卡中写入相关的数据 3.操作非常简单,只需要在在Activity中的onResume、onPause、onDestroy、onNewIntent方法中调用nfc工具类的相关方法就ok了,完整的步骤请查看demo

    Android 退出多Activity的application的方式方法

    在开发过程中,我们常常需要一个退出功能,来退出该应用的所有Activity。下面,我们列举一些退出应用的几种方式。 1.利用ActivityContainer来管理所有的Activity的引用 2.使用广播通知BaseActivity结束 3.直接杀...

    详解Activity之singletast启动模式及如何使用intent传值

    Activity的四种启动模式:  1. standard ... 如果在栈中已经有该Activity的实例,就重用该实例(会调用实例的onNewIntent())。重用时,会让该实例回到栈顶,因此在它上面的实例将会被移除栈。如果

    XFragment:单Activity和多Fragment容器

    并且也支持特地的fragemnt* 支持fragment跳转的launch mode,目前支持standard,singleTop和singleTask,并且模拟了类似activity的onNewIntent()* 支持类似activity的onActivityResult()的回调,fragment采用了监听...

    如何正确理解和使用Activity的4种启动模式

    关于Activity启动模式的文章已经很多,但...一个Android应用一般都会有多个Activity,系统会通过任务栈来管理这些Activity,栈是一种后进先出的集合,当前的Activity就在栈顶,按返回键,栈顶Activity就会退出。Activi

    open-url-in-qt-android:迷你概念证明示例,如何通过单击链接在Android上启动QT应用程序

    实现onNewIntent(Intent intent) onNewIntent(Intent intent) 对QT的C ++端的JNI调用public static native void setUrl(String url) C ++实现openurlclient.cpp JNI调用的实现 JNIEXPORT void JNICALL Java_...

    360黑科技DroidPlugin.zip

    Activity的onNewIntent函数可能不会被触发。 (此为BUG,未来会修复)缺乏对Native层的Hook,对某些带native代码的apk支持不好,可能无法运行。比如一部分游戏无法当作插件运行。特点:支持Androd 2.3以上系统插件...

    Android实现读取NFC卡卡号示例

    Android实现读取NFC卡卡号示例,具体如下: 1.权限 <uses android:name=android.permission.NFC> <uses android:name=android.hardware.nfc android:required=true> 2.注册(静态) <action android:name=...

    android-uri-dispatcher

    UriDispatcher UriDispatcher是适用于Android的库。 调度到由注释分配的匹配方法。如何使用使用uri字符串或intent实例调用UriDispatcher#dispatch 。 @Overrideprotected void onNewIntent( final Intent intent) { ...

    DroidPlugin插件机制

    Activity的onNewIntent函数可能不会被触发。 (此为BUG,未来会修复) 缺乏对Native层的Hook,对某些带native代码的apk支持不好,可能无法运行。比如一部分游戏无法当作插件运行。 特点: 支持Androd 2.3以上系统 ...

Global site tag (gtag.js) - Google Analytics