一、onNewIntent()

在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestroy  onNewIntent

1、其他应用发Intent,执行下列方法:
onCreate
onStart
onResume

发Intent的方法:

1 Uri
uri = Uri.parse(
"philn://blog.163.com");
2 Intent
it = 
new Intent(Intent.ACTION_VIEW,
uri);   
3 startActivity(it);

2、接收Intent声明:

1 <activity android:name=".IntentActivity" android:launchMode="singleTask"
2                   android:label="@string/testname">
3              <intent-filter>
4                 <action android:name="android.intent.action.VIEW" />
5                 <category android:name="android.intent.category.DEFAULT" />
6                 <category android:name="android.intent.category.BROWSABLE" />
7                 <data android:scheme="philn"/>
8             </intent-filter>
9 </activity>

3、如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于onPause、onStop状态的话,其他应用再发送Intent的话,执行顺序为:
onNewIntent,onRestart,onStart,onResume。

在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"></activity>

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

1 protected void onNewIntent(Intent
intent) {
2  
3   super.onNewIntent(intent);
4  
5   setIntent(intent);//must
store the new intent unless getIntent() will return the old one
6  
7   processExtraData();
8  
9 }

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

1 public void onCreate(Bundle
savedInstanceState) {
2  
3   super.onCreate(savedInstanceState);
4  
5   setContentView(R.layout.main);
6  
7   processExtraData();
8  
9 }
1 protected void onNewIntent(Intent
intent) {
2  
3   super.onNewIntent(intent);
4  
5    setIntent(intent);//must
store the new intent unless getIntent() will return the old one
6  
7   processExtraData()
8  
9 }
1 private void processExtraData(){
2  
3   Intent
intent = getIntent();
4  
5   //use
the data received here
6  
7 }

二、onNewIntent()的setIntent()和getIntent()

1 @Override
2 protected void onNewIntent(Intent
intent) {
3     super.onNewIntent(intent);
4  
5     //
setIntent(intent);
6  
7     int data
= getIntent().getIntExtra(
"HAHA"0);
8     //
int data = intent.getIntExtra("HAHA", 0);
9 }

如果没有调用setIntent(intent),则getIntent()获取的数据将不是你所期望的。但是使用intent.getInXxx,貌似可以获得正确的结果。

注意这句话:
Note that getIntent() still returns
the original Intent. You can use setIntent(Intent) to update it to this new Intent.

所以最好是调用setIntent(intent),这样在使用getIntent()的时候就不会有问题了。

【转】android onNewIntent()触发机制及注意事项的更多相关文章

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

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

  2. Android消息机制使用注意事项,防止泄漏

    在Android的线程通信当中,使用频率最多的就是Android的消息处理机制(Handler.send().View.post().Asynctask.excute()等等都使用到了消息处理机制). ...

  3. 讲讲Android事件拦截机制

    简介 什么是触摸事件?顾名思义,触摸事件就是捕获触摸屏幕后产生的事件.当点击一个按钮时,通常会产生两个或者三个事件--按钮按下,这是事件一,如果滑动几下,这是事件二,当手抬起,这是事件三.所以在And ...

  4. Android事件分发机制(下)

    这篇文章继续讨论Android事件分发机制,首先我们来探讨一下,什么是ViewGroup?它和普通的View有什么区别? 顾名思义,ViewGroup就是一组View的集合,它包含很多的子View和子 ...

  5. Android事件分发机制(上)

    Android事件分发机制这个问题不止一个人问过我,每次我的回答都显得模拟两可,是因为自己一直对这个没有很好的理解,趁现在比较闲对这个做一点总结 举个例子: 你当前有一个非常简单的项目,只有一个Act ...

  6. Android事件分发机制完全解析,带你从源码的角度彻底理解

    Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和多指操作.所有这些都构成了Android中的事件响应.总的来说,所有的事件都 ...

  7. Android的事件处理机制详解(二)-----基于监听的事件处理机制

    基于监听的事件处理机制 前言: 我们开发的app更多的时候是需要与用户的交互----即对用户的操作进行响应 这就涉及到了android的事件处理机制; android给我们提供了两套功能强大的处理机制 ...

  8. [转]Android事件分发机制完全解析,带你从源码的角度彻底理解(上)

    Android事件分发机制 该篇文章出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分 ...

  9. 弄明白Android 接口回调机制

    以前对于这个机制理解不够深刻,现在重新整理下思路. 一.建模 我理解的接口回调就是,我这个类实现了一个接口里的方法doSomething,然后注册到你这里,然后我就去做别的事情去了,你在某个触发的时机 ...

随机推荐

  1. Android Studio 学习 - 基本控件的使用;Intent初学

    Android Studio学习第三天. 今天主要学习 1. RadioButton.CheckBox.RatingBar.SeekBar等基础控件的使用. 结合Delphi中相类似的控件,在这些基本 ...

  2. Hadoop学习总结之三:Map-Reduce入门

    1.Map-Reduce的逻辑过程 假设我们需要处理一批有关天气的数据,其格式如下: 按照ASCII码存储,每行一条记录 每一行字符从0开始计数,第15个到第18个字符为年 第25个到第29个字符为温 ...

  3. nodejs学习--express篇

    express篇:http://www.runoob.com/nodejs/nodejs-express-framework.html Express 提供了内置的中间件 express.static ...

  4. phpmyadmin 设置用户登录

    找到 /opt/lampp/phpmyadmin/config.inc.php文件修改下面配置 这是原配置 #$cfg['Servers'][$i]['auth_type'] = 'config'; ...

  5. centos下安装eclipse-c++

    eclipse-c++ 1)编译器及工具链 yum install gcc gcc-c++ 2)开发工具包(JDK):下载网址:http://www.oracle.com/technetwork/ja ...

  6. Visual Studio 2005 移植 - WINVER,warning C4996, error LINK1104

    Visual Studio 2005 移植 - WINVER,warning C4996, error LINK1104 一.WINVER  Compile result:  WINVER not d ...

  7. PyPI镜像网站

    镜像列表:http://www.pypi-mirrors.org/ 清华镜像:http://e.pypi.python.org/

  8. CSS框架分析与网站的CSS架构

    框架(framework)是一个基本概念上的结构,用于去解决或者处理复杂的问题,是一种可复用的构架. 我们对CSS框架这个词比较陌生,但对于JavaScript框架就比较熟悉了,比如jQuery 但为 ...

  9. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  10. windows下跑python flask,环境配置

    首先声明一下,我安装的是python 2.7. 第一步:下载easy_setup.py 下载地址:https://pypi.python.org/pypi/setuptools 这个下载地址真心难找, ...