android onNewIntent
在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的更多相关文章
- android onNewIntent调用时机
(转自:http://www.cnblogs.com/zenfly/archive/2012/02/10/2345196.html) 在IntentActivity中重写下列方法:onCreate o ...
- 【转】android onNewIntent()触发机制及注意事项
一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestro ...
- android onNewIntent 为什么要在onNewIntent的时候要显示的去调用setIntent
原因: 当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getInte ...
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验: androi ...
- 【Android】onNewIntent调用时机
在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent一.其他应 ...
- android Intent onNewIntent 什么时候调用
1.activity A 的lanch model 为singleTop 此刻,A在activity 栈顶,那么就会调用A 的onNewIntent 如果A不在栈顶,则不会调用. 2.activity ...
- Android:onNewIntent()触发机制及注意事项
一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestro ...
- 解决Android singleTask模式下PendingIntent不能给onNewIntent传值的Bug
http://phenix.blogbus.com/logs/220656659.html 博主简直碉堡了, 我用PendingIntent给singleTask的顶层Activity传值一直收不到, ...
- Android调用微信登陆、分享、支付
前言:用了微信sdk各种痛苦,感觉比qq sdk调用麻烦多了,回调过于麻烦,还必须要在指定包名下的actvity进行回调,所以我在这里写一篇博客,有这个需求的朋友可以借鉴一下,以后自己别的项目有用到也 ...
随机推荐
- 解决play-1.4.0在linux或mac下提示No such file or directory的问题
问题原因:"play"脚本中有特殊符号. 解决方案:写脚本去掉即可. 代码:fixplay.py 放在play-1.4.0目录下执行.亲测在osx与ubuntu下均可用. with ...
- tmux protocol version mismatch (client 7, server 6)
$ tmux attach protocol version mismatch (client 7, server 6) $ pgrep tmux 3429 $ /proc/3429/exe atta ...
- wordpress添加文章浏览统计(刷新不重复)
wordpress本身不带文章浏览统计,可以用插件wp-postview,但是刷新还是算一个浏览次数. 1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的 //add ...
- debian+apache+acme_tiny+lets-encrypt配置笔记
需要预先将需要申请ssl的域名指向到服务器,此方法完全通过api实现,好处是绿色无污染,不需要注册账号,不会泄露私人信息环境为 debian7+apache apt-get install apach ...
- php利用smtp发送邮件
PHP : 5.6.8 email工具类下载地址: http://files.cnblogs.com/files/rhythmK/email.class.zip 发送邮件代码如下: require_o ...
- Top100Summit全球案例研究峰会第一天总结——云计算和大数据
很荣幸受邀参加Top100Summit全球软件案例研究峰会,这次的大会主题是<技术推动商业变革>,组委会从全国投稿的460多件案例中甄选出100件具有代表价值的案例,进行为期4天的分享,第 ...
- 【转】在Eclipse里查看Java字节码
要理解 Java 字节码,比较推荐的方法是自己尝试编写源码对照字节码学习.其中阅读 Java 字节码的工具必不可少.虽然javap可以以可读的形式展示出.class 文件中字节码,但每次改动源码都需调 ...
- When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
The most important difference is that the forIndexPath: version asserts (crashes) if you didn't regi ...
- ubuntu14.04使用IceGridAdmin图形界面
打开网页: http://www.rpmfind.net/linux/RPM/index.html输入搜索: icegrid-gui下载文件: icegrid-gui-3.5.1-2.mga4.x86 ...
- Testing - Selenium
Selenium http://www.seleniumhq.org/ User Guide http://www.seleniumhq.org/docs/ Webdriver中文社区 http:// ...