Activity的四种启动模式和onNewIntent()的使用
来源:互联网 发布:git ignore mac 编辑:程序博客网 时间:2024/04/30 04:38
Activity的四种启动模式和onNewIntent()的使用
一、问题的描述
当Activity , A的启动模式被设置为singleTask时,在第一次启动后未被销毁仍在栈中,当有一个携带有数据的Intent,再次启动Activity ,A时。A在生命周期的哪个复写的回调方法中去接收新的Intent传递来的的数据并处理?
二、问题的解答
在onNewIntent()方法中。代码实例如下
<span style="white-space:pre"></span>@Overridepublic void onNewIntent(Intent intent){super.onNewIntent(intent);//must store the new intent unless getIntent() will return the old onesetIntent(intent);processExtraData();}/** * 获得Intent传递来的数据并弹出显示 */public void processExtraData() {String test=getIntent().getStringExtra("test");textView.setText(test);Toast.makeText(this, test, Toast.LENGTH_LONG).show();}
三、问题的分析(为什么?)
1.Activity的四种启动模式(Android使用返回栈来管理活动)
(1)standard
活动默认的启动模式,在不进行显示指定的情况下,所有活动都会自动使用这种模式。
特点:系统不在乎这个活动是否已经在返回栈中存在,每次启动都会创建该活动的一个新的实例
(2)singleTop
特点:在启动活动时,如果发现栈顶已经是该活动,则认为可以直接使用它,不会再创建
(3)singleTask
特点:在启动活动时,会首先在返回栈中检查是否存在该活动的实例,如果发现已经存在则直接使用,并把在这个活动之上的所有活动都统统出栈。
(4)singleInstance
特点:指定这种模式的活动,会启用一个新的返回栈来管理这个活动。主要是为了实现其他应用程序和本应用程序共享这个活动实例。
2.Activity第一次启动和已在返回栈中的启动流程
(1)Activity的第一次(创建启动)流程
onCreate()->onStart()->onResume()
(2)已在栈中singleTask模式的Activity 在Intent触发下启动流程
onNewIntent()->onRestart()->onStart()->onResume()
3.问题的产生于解决方法
由于第一次启动singleTask模式的Activity会走onCreate()方法,此时可以在onCreate中去处理Intent传过来的数据。但是第二次Intent去启动时并不会新建Activity,所以,处理传过来的数据无法在onCreate中处理。只能放在onNewIntent()中去处理,这样一综合,可以在onCreate()和onNewIntent()中同时都去调用一个数据处理的方法
疑惑点:为什么不将数据处理放到onStart()或者onResume()中去呢?
因为:
1.当Activity不是用Intent触发启动,仅仅是因为覆盖在它上面的Activity回退时将Activity放到前台时,也会调用onStart()
和onResume()方法,造成了数据的重复处理。
2.如果不在onNewIntent()中去处理新的Intent 并将其存储时,以后再onStart()等方法中取得的Intent将会是以前的Intent。
四、参考资料
1.Activity的四种启动模式和onNewIntent():http://blog.csdn.net/linghu_java/article/details/17266603
2.
Activity的OnNewIntent()调用时机
:http://blog.sina.com.cn/s/blog_9eb164850102v1om.html
0 0
- Activity的四种启动模式和onNewIntent()的使用
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Android--Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- [置顶] Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- Activity的四种启动模式和onNewIntent()
- nodejs sqlite 数据库操作
- 网易视频云分享:最佳日志实践
- Struts2自定义标签——通过自定义标签实现权限控制
- 拉手网_php-fpm负载飙升解决实战
- openssh升级
- Activity的四种启动模式和onNewIntent()的使用
- 设置UILabel可变高度(根据文本内容自动适应高度)
- 1 个无法解析的外部命令
- python libsvm windows 64位 安装
- google test 轻松编写C++单元测试
- Centos内存管理-free命令
- Android 混淆代码总结
- 服务器运维脚本记录
- 15 个 Android 通用流行框架大全