一、原因:
当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIntent()都是得到老的Intent。 二、onNewIntent()
在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent 1、其他应用发Intent,执行下列方法:
onCreate
onStart
onResume
发Intent的方法:
Uri uri = Uri.parse("philn://blog.163.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it) 2、接收Intent声明:
<activity android:name=".IntentActivity" android:launchMode="singleTask"
android:label="@string/testname">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="philn"/>
</intent-filter>
</activity> 3、如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于onPause、onStop 状态的话,其他应用再发送Intent的话,执行顺序为:
onNewIntent,onRestart,onStart,onResume。
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是需要让后台运行的Activity回到前台并传递一些数据可能就会存在一点点小问题。
首先,在默认情况下,通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来。通过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,如下所示:
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity> launchMode为singleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再调用通常情况下处理请求数据的onCreate方法,而是调用onNewIntent方法,如下所示:
protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);//must store the new intent unless getIntent() will return the old one processExtraData(); } 不要忘记,系统可能会随时杀掉后台运行的 Activity ,如果这一切发生,那么系统就会调用 onCreate 方法,而不调用 onNewIntent 方法,一个好的解决方法就是在 onCreate 和 onNewIntent 方法中调用同一个处理数据的方法,如下所示:public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); processExtraData(); }
protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);//must store the new intent unless getIntent() will return the old one processExtraData() }
private void processExtraData(){ Intent intent = getIntent(); //use the data received here }

  

一、原因:
当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIntent()都是得到老的Intent。

二、onNewIntent()
在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent

1、其他应用发Intent,执行下列方法:
onCreate
onStart
onResume
发Intent的方法:
Uri uri = Uri.parse("philn://blog.163.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(it)

2、接收Intent声明:
<activity android:name=".IntentActivity" android:launchMode="singleTask"
android:label="@string/testname">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="philn"/>
</intent-filter>
</activity>

3、如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于onPause、onStop 状态的话,其他应用再发送Intent的话,执行顺序为:
onNewIntent,onRestart,onStart,onResume。
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是需要让后台运行的Activity回到前台并传递一些数据可能就会存在一点点小问题。
首先,在默认情况下,通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来。通过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,如下所示:
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity>

launchMode为singleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再调用通常情况下处理请求数据的onCreate方法,而是调用onNewIntent方法,如下所示:
protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

setIntent(intent);//must store the new intent unless getIntent() will return the old one

processExtraData();

}

不要忘记,系统可能会随时杀掉后台运行的 Activity ,如果这一切发生,那么系统就会调用 onCreate 方法,而不调用 onNewIntent 方法,一个好的解决方法就是在 onCreate 和 onNewIntent 方法中调用同一个处理数据的方法,如下所示:public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

processExtraData();

}
protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

setIntent(intent);//must store the new intent unless getIntent() will return the old one

processExtraData()

}
private void processExtraData(){

Intent intent = getIntent();

//use the data received here

}

为什么要在onNewIntent的时候要显示的去调用setIntent的更多相关文章

  1. android onNewIntent 为什么要在onNewIntent的时候要显示的去调用setIntent

    原因: 当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getInte ...

  2. IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型

    ********HMVideosViewController.m #import "HMVideosViewController.h" #import "MBProgre ...

  3. Android应用程序中的多个Activity的显示创建和调用

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTkzNjE0Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. 020、MySQL创建一个存储过程,显示存储过程,调用存储过程,删除存储过程

    一.我们创建一个MySQL储存过程,在SQL代码区写入以下内容,并执行就可以了 #编写一个存储过程 CREATE PROCEDURE ShowDate ( ) BEGIN #输出当前时间 SELECT ...

  5. Office——EXCEL 显示 隐藏 Ribbon 调用宏

    ==================================声明================================== 本文版权归作者所有 未经作者授权 请勿转载 保留法律追究的 ...

  6. 切换用户,显示用户名, 调用Windows系统命令

    CONN 用户名/密码 CONN sys/密码 AS SYSDBA | SYSOPER SHOW USER SELECT * FROM 用户名.表名; 调用Windows系统命令: HOST Wind ...

  7. python之路六

    面向对象 引言 提到面向对象,总是离不开几个重要的术语:多态(Polymorphism),继承(Inheritance)和封装(Encapsulation).Python也是一种支持OOP的动态语言, ...

  8. java中InvocationHandler 用于实现代理。

    以下的内容部分参考了网络上的内容,在此对原作者表示感谢! Java中动态代理的实现,关键就是这两个东西:Proxy.InvocationHandler,下面从InvocationHandler接口中的 ...

  9. Java问题:Quartz,Hibernate,Spring,Tomcat中定时任务无故停止,没有错误

    最近在做一个java项目的时候遇到一个十分奇怪的问题,想到大家可能也会遇到这样的问题,所以在此发出来,希望大家遇到的时候能够快速解决! 直入主题 问题:使用quartz进行定时任务自动执行的时候,用到 ...

随机推荐

  1. memcached and redis

    http://hzp.iteye.com/blog/1872664 http://www.diggerplus.org/archives/190 Redis

  2. 第七十六节,css颜色和透明度,盒子阴影和轮廓,光标样式

    css颜色和透明度,盒子阴影和轮廓,光标样式 学习要点: 1.颜色和透明度 2.盒子阴影和轮廓 3.光标样式 一.颜色和透明度 颜色我们之前其实已经用的很多了,比如字体颜色.背景颜色.边框颜色.但除了 ...

  3. 《JS权威指南学习总结--8.8.4 记忆函数》

    内容要点:   可以将上次的计算结果缓存起来.在函数式编程当中,这中缓存技巧叫做 "记忆". 需要注意的是,记忆只是一种编程技巧,本质上是牺牲算法的空间复杂度以换取更优的时间复杂度 ...

  4. PAT 团体程序设计天梯赛-练习集 L1-005. 考试座位号

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

  5. HDU2037今年暑假不AC(贪心)

    Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACM ...

  6. 3DTouch--2

    苹果太贱了! 3D Touch 只能在真机上面试,模拟器没办法玩! ------------- 描述有点粗燥.....有6S 在手上玩得童鞋会更加清楚,只有玩过才更加体验到. 首先 有几个要知道的手势 ...

  7. Unity5系列资源管理AssetBundle——打包

    资源管理是游戏开发的重要环节,Unity中使用AssetBundle可以非常方便地帮我们打包和更新游戏内容,在5系列中,AssetBundle更是方便好用,现在让我们先进行打包吧. 刚说了,5系列打包 ...

  8. chapter8_3 c代码和错误

    1.C代码 Lua提供的所有关于动态链接的功能都集中在一个函数中,即package.loadlib. 该函数有两个字符串参数:动态库的完整路径和一个函数名称: local path = "/ ...

  9. rabbitmq——镜像队列

    转自:http://my.oschina.net/hncscwc/blog/186350?p=1 1. 镜像队列的设置 镜像队列的配置通过添加policy完成,policy添加的命令为: rabbit ...

  10. Hadoop作业优化

    mapper数量 reduce数量 combiner 中间值压缩 自定义序列 调整shuffle,减少溢出写 关闭推测执行 任务JVM重用 慢启动reduce