android中在onNewIntent实现刷新数据

来源:互联网 发布:小米3能用联通4g网络吗 编辑:程序博客网 时间:2024/05/21 07:55

一、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();

 

如果这时候一个Activity1上是一个请求数据的列表,当打开一个新的Activity2去新建或者修改一条列表数据时候,finish掉Activity2的时候要刷新Acivity1的列表数据,根据以上Activity的生命周期以及onNewIntent()的机制可以将刷新数据的方法或者重新请求数据的方法放到onNewIntent()中,将Activity的launchMode设置为singleTask就可以实现这个效果。
阅读全文
0 0
原创粉丝点击