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

来源:互联网 发布:iphonex 壁纸编辑软件 编辑:程序博客网 时间:2024/06/14 02:07

一、onNewIntent()

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


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

Intent的方法:

1Uri uri = Uri.parse("philn://blog.163.com");
2Intent it = new Intent(Intent.ACTION_VIEW, uri);   
3startActivity(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>


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

1protected 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 方法中调用同一个处理数据的方法,如下所示:
1public void onCreate(Bundle savedInstanceState) {
2 
3  super.onCreate(savedInstanceState);
4 
5  setContentView(R.layout.main);
6 
7  processExtraData();
8 
9}
1protected 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}
1private void processExtraData(){
2 
3  Intent intent = getIntent();
4 
5  //use the data received here
6 
7}

 

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

1@Override
2protected 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()的时候就不会有问题了。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝吸收不好长不胖怎么办 一岁半宝宝吸收不好怎么办 婴儿看见脏东西怎么办 母乳变质吃了怎么办 六个月婴儿积食怎么办 儿童吸收功能差怎么办 小孩吸收不好长不胖怎么办 宝贝喝水都吐怎么办 小孩子便秘大便硬结怎么办 一岁半宝宝不爱吃饭怎么办 小孩生病不吃饭怎么办 宝宝吃药不肯咽怎么办 宝宝自己不吃饭怎么办 宝宝突然不想吃饭怎么办 孩子死活不吃药怎么办 小孩死活不吃药怎么办 婴儿不吃药怎么办啊 1周岁宝宝厌食怎么办 宝宝吃药会吐怎么办 宝宝上火拉不出大便怎么办 40天宝宝上火怎么办 宝宝8个月结火了怎么办 婴儿喝牛奶上火怎么办 儿童喝牛奶上火怎么办 三岁宝宝不喝奶怎么办 孩子喝牛奶过敏怎么办 小孩哭晕过去怎么办 宝宝断不了奶怎么办 小孩喝牛奶过敏怎么办 宝宝不爱喝牛奶怎么办? 孕妇喝牛奶恶心怎么办 孕妇喝奶粉上火怎么办 宝宝断奶一直哭怎么办 孕妇喝不了牛奶怎么办 断奶时孩子哭闹怎么办 宝宝断奶夜里哭怎么办 小孩断奶晚上哭怎么办 断奶小孩一直哭怎么办 孕妇喝不下鸡蛋怎么办 断奶涨奶怎么办能挤吗 宝宝不吃的奶粉怎么办