[Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
来源:互联网 发布:淘宝助理天猫版本 编辑:程序博客网 时间:2024/06/06 01:58
问题来自这儿,Bundle在接收时未更新,http://blog.csdn.net/dadoneo/article/details/8164058。
虽然可以暂时解决问题,但并未说到根本原因,下面就Activity的LaunchMode来说说这个Bundle到底要怎么更新。
============================================
用如下方式打开Activity并传递参数
Intent i = new Intent(this, ImgInfo.class); i.putExtra("id", mPhotoId); startActivity(i); Intent i = new Intent(this, ImgInfo.class);i.putExtra("id", mPhotoId); startActivity(i);
如果不了解singleInstance的原理(之前并未注意到是这的问题),我们想当然地会在接收端Activity的onResume或其它方法中写下如下的代码:
Bundle bud = getIntent().getExtras(); if (bud != null && bud.containsKey("id")) { mPhotoId = bud.getInt("id"); } Bundle bud = getIntent().getExtras();if (bud != null && bud.containsKey("id")) {mPhotoId = bud.getInt("id");}
传递不同的id,你就会发现除了第一次能正确接收之外,其他的好像都不行了。其实就是没有更新,需要怎么做呢?
这时,我们需要重写接收端Activity的onNewIntent()方法,如下:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); InitArg(); }
看看adnroid doc怎么说:
protected void onNewIntent (Intent intent)Added in API level 1This 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.ParametersintentThe new intent that was started for the activity.
其实就是:
onCreate是用来创建一个Activity也就是创建一个窗体,但一个Activty处于任务栈的顶端,若再次调用startActivity去创建它,则不会再次创建。若你想利用已有的Acivity去处理别的Intent时,你就可以利用onNewIntent来处理。在onNewIntent里面就会获得新的Intent.
如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于
onPause
onStop 状态的话
其他应用再发送Intent的话,执行顺序为:
onNewIntent
onRestart
onStart
onResume
http://marshal.easymorse.com/archives/2950
- [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
- [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
- [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
- Android 以singleInstance模式加载的Activity怎么接收以putExtra方式传递过来参数
- Android 以singleInstance模式加载的Activity怎么接收以putExtra方式传递过来参数
- 以singleTask或singleInstance方式加载的activity如何接收intent传递过来的的参数
- 以singleTask或singleInstance方式加载的activity如何接收intent传递过来的的参数
- 以singleTask或singleInstance方式加载的activity如何接收intent的附加数据
- Activity的加载模式之singleInstance——Android
- Android -singleInstance 关于activity 的存在模式
- 通过代理Activity模式,以移花接木的方式,加载sd卡目录下的apk界面
- 通过代理Activity模式,以移花接木的方式,加载sd卡目录下的apk界面
- 接收以流的形式传输过来的数据
- singleTask模式的Activity接收Bundle问题
- 以引用的方式传递参数避免对象切割
- Window.location.href以post方式传递参数的方法
- Window.location.href以post方式传递参数的方法
- Spring MVC中action接收从页面传递过来的参数方式
- 冲突域与广播域(区别、知识要点)
- MySQLWorkbench里的稀奇事之timestamp的非空默认值
- 宏开关管理模块的实现[C语言]
- PHP设计模式——工厂方法模式(研磨设计模式学习笔记)
- Jedis使用中遇到的一些问题timeout no connection
- [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
- 设计模式学习笔记(七)之适配器模式(Adapter)
- 淘宝数据魔方技术架构解析
- 免费开源的虚拟机软件VirtualBox
- Logitech G27 程序读取数据
- 穷举法解数独
- HTTP 状态消息
- objdump(GCC)命令的用法
- Java Web