Why should onNewIntent time to be displayed to call setIntent

Original link: http://www.cnblogs.com/wikiki/p/5191134.html
一、原因:
当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIntent()都是得到老的Intent。

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

1、其他应用发Intent,执行下列方法:
onCreate
onStart
onResume
发Intent的方法:
Uri uri = Uri.parse("philn://blog.163.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(it)

2、接收Intent声明:
<activity android:name=".IntentActivity" android:launchMode="singleTask"
android:label="@string/testname">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="philn"/>
</intent-filter>
</activity>

3、如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于onPause、onStop 状态的话,其他应用再发送Intent的话,执行顺序为:
onNewIntent,onRestart,onStart,onResume。
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是需要让后台运行的Activity回到前台并传递一些数据可能就会存在一点点小问题。
首先,在默认情况下,通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来。通过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,如下所示:
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity>

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

super.onNewIntent(intent);

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

processExtraData();

}

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

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

processExtraData();

}
protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

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

processExtraData()

}
private void processExtraData(){

Intent intent = getIntent();

//use the data received here

}

  

First, the reason:
when calling to onNewIntent (intent) when the need to use setIntent in onNewIntent () in the (intent) assigned to Intent Activity otherwise, subsequent getIntent () to get all the old Intent..

Two, onNewIntent ()
override IntentActivity the following methods: the onCreate the onStart onRestart onResume the onPause onStop onDestroy onNewIntent

. 1, other hair Intent application, perform the following method:
the onCreate
the onStart
onResume
method of hair Intent:
Uri URI = Uri.parse ( "philn: //blog.163.com ");
Intent new new IT = Intent (Intent.ACTION_VIEW, URI); 
startActivity (IT)

2, receiving Intent statement:
. <Android Activity: name =" IntentActivity "Android: the launchMode =" singleTask "
Android: label = "@ String / the testname">
<filter-Intent>
<Action Android: name = "android.intent.action.VIEW" />
<category Android: name = "android.intent.category.DEFAULT" />
<category Android: name = "android.intent.category.BROWSABLE" />
<Data Android: scheme = "philn" />
</ filter-Intent >
</ Activity>

. 3, if IntentActivity at the top of the task stack, that is opened before Activity, now in onPause, onStop state, then the other resending Intent, then execution order:
onNewIntent, onRestart, the onStart, onResume .
When Android application development, from one Activity to start another Activity and pass some data to the new Activity is very simple, but the need to run in the background Activity returned to the forefront and pass some data might exist a little small problem.
First, by default, to start an Activity by Intent time, even if there is already an identical Activity is running, the system will create a new Activity instance and displayed. By AndroidManifest.xml activity of loading configuration (the launchMode) to achieve a single-tasking mode, as follows:
<activity Android: label = "@ String / APP_NAME" Android: launchMode = "singleTask" Android: name = "

singleTask launchMode as when to start by a Intent Activity, if the system already exists one example, the system sends a request to this example, but this time, the system will not usually call requesting data processing method onCreate It will be called onNewIntent method, as follows:
protected void onNewIntent (the Intent Intent) {

super.onNewIntent (Intent);

setIntent (Intent); // The new new MUST Intent The unless its getIntent Store () The Old One Will return

processExtraData () ;

}

Do not forget, you may be ready to kill Activity running in the background, if this happens, then the system will call onCreate method, without calling onNewIntent method, a good solution is to call the same method in onCreate and onNewIntent the method of processing data, as follows: public void the onCreate (the Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

the setContentView (R.layout.main);

processExtraData ();

}
protected void onNewIntent (the Intent Intent) {

super.onNewIntent ( intent);

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

processExtraData()

}
private void processExtraData(){

Intent intent = getIntent();

//use the data received here

}

Reproduced in: https: //www.cnblogs.com/wikiki/p/5191134.html

Guess you like

Origin blog.csdn.net/weixin_30457465/article/details/94788367