android singleTask onNewIntent

在Android应用程序开发的时候,从一个Activity启动另外一个Activity并传递一些数据到新的Activity上很是简单,可是当您须要让后台运行的Activity回到前台并传递一些数据可能就会存在一点点小问题。android

首先,在默认状况下,当您经过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会建立一个新的Activity实例并显示出来。为了避免让Activity实例化屡次,咱们须要经过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,以下所示:app

 

<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity>

 

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

复制代码

 protected void onNewIntent(Intent intent) {

   super.onNewIntent(intent);

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

   processExtraData();

 }
相关文章
相关标签/搜索