在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就会存在一点点小问题。

首先,在默认情况下,当您通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来。为了不让Activity实例化多次,我们需要通过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,如下所示:

1 <activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1">

2 </activity>

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

1 protected void onNewIntent(Intent intent) {

2   super.onNewIntent(intent);

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

4   processExtraData();

5 }

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

01 public void onCreate(Bundle savedInstanceState) {

02   super.onCreate(savedInstanceState);

03   setContentView(R.layout.main);

04   processExtraData();

05 }

06

07 protected void onNewIntent(Intent intent) {

08   super.onNewIntent(intent);

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

10   processExtraData()

11 }

12

13 private void processExtraData(){

14   Intent intent = getIntent();

15   //use the data received here

16 }

android onNewIntent的更多相关文章

  1. android onNewIntent调用时机

    (转自:http://www.cnblogs.com/zenfly/archive/2012/02/10/2345196.html) 在IntentActivity中重写下列方法:onCreate o ...

  2. 【转】android onNewIntent()触发机制及注意事项

    一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestro ...

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

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

  4. android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法

    最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验: androi ...

  5. 【Android】onNewIntent调用时机

    在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent一.其他应 ...

  6. android Intent onNewIntent 什么时候调用

    1.activity A 的lanch model 为singleTop 此刻,A在activity 栈顶,那么就会调用A 的onNewIntent 如果A不在栈顶,则不会调用. 2.activity ...

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

    一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestro ...

  8. 解决Android singleTask模式下PendingIntent不能给onNewIntent传值的Bug

    http://phenix.blogbus.com/logs/220656659.html 博主简直碉堡了, 我用PendingIntent给singleTask的顶层Activity传值一直收不到, ...

  9. Android调用微信登陆、分享、支付

    前言:用了微信sdk各种痛苦,感觉比qq sdk调用麻烦多了,回调过于麻烦,还必须要在指定包名下的actvity进行回调,所以我在这里写一篇博客,有这个需求的朋友可以借鉴一下,以后自己别的项目有用到也 ...

随机推荐

  1. C#壓縮文件幫助類 使用ICSharpCode.SharpZipLib.dll

    using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using System; using Syst ...

  2. c# 垮线程调用控件

    http://www.cnblogs.com/TankXiao/p/3348292.html

  3. 数据库必会必知 之 SQL四种语言:DDL DML DCL TCL

    作者:泥瓦匠 今天群里面讨论,DDL 还是 DML,我这种小白还是总结下他们的区别吧. 1. DDL - Data Definition Language 数据库定义语言:定义数据库的结构. 其主要命 ...

  4. ivqBlog 开源博客 (angularjs + express + mongodb)

    转向做全职前端差不多一年的时间了,其中学习了构建工具grunt,gulp,angularjs,coffeescript,less,sass,自己想要做全栈开发,所以自学了mongodb,nodejs, ...

  5. Idea 201601注册码

    参考链接 http://blog.csdn.net/u010310183/article/details/51162137 在license 下面输入 http://www.iteblog.com/i ...

  6. 如何使用 App Studio 快速定制你自己的 Universal Windows App

    之前我为大家介绍过 App Studio 这只神器可以帮助大家快速制作一个 Windows Phone 8 的应用,今天之所以在写一篇关于 App Studio 的文章是因为,App Studio 经 ...

  7. sublime返回上一编辑位置

    用了sublime好长时间了,最近发现一个python插件可以在编辑的时候返回上一编辑位置,这个功能在eclipse很常用,现在终于能在sublime上使用了.好爽. 贴个地址:https://for ...

  8. PowerPoint 打开文档发现.pptx中胡内容有问题

    一.问题的提出 有一个文件,在window 7操作系统中通过邮箱地址保存到本地,结果打开的时候出现[PowerPoint 打开文档发现 文件.pptx中胡内容有问题] 然后提示[如果您信任此演示文稿的 ...

  9. 游戏开发工具之纹理打包器-3.使用GDI+绘图

    上一次我们实现了把我们要的图片添加到CTreeCtrl控件里去,并显示图片的缩略图,现在开始我们要讲比较重要的部分--绘图区.为了实现能编辑图片的功能,绘图区应该具有如下功能. 1.  添加删除图片. ...

  10. Go Go

    Go Go *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...