[Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
来源:互联网 发布:word2010表格数据缺失 编辑:程序博客网 时间:2024/05/29 17:20
问题来自这儿,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 1
- 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 The 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
版权声明:http://blog.csdn.net/dadoneo/article/details/8170124
- [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接收从页面传递过来的参数方式
- malloc、calloc、realloc的区别
- android使用代码编写布局
- NoSQL架构实践(一)——以NoSQL为辅
- iphone6 plus以下设备适配经验总结
- 开始记录
- [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum
- Python的深拷贝与浅拷贝
- 喜欢玩warcraft的ltl
- OC学习笔记03-OC中访问修饰符,点语法与@property,@synthesize关键字
- STP选举实例
- 欢迎使用CSDN-markdown编辑器
- NoSQL架构实践(二)——以NoSQL为主
- c#根据公式进行自动计算的实现
- (转)struts2 Action获取表单数据