没有合适的资源?快使用搜索试试~ 我知道了~
Android onNewIntent()触发机制及注意事项
0 下载量 139 浏览量
2021-01-05
19:20:45
上传
评论
收藏 54KB PDF 举报
温馨提示
试读
2页
一、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、接收Inten
资源推荐
资源详情
资源评论
Android onNewIntent()触发机制及注意事项触发机制及注意事项
一、一、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实例并显示出来。为了不让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) {
资源评论
weixin_38597533
- 粉丝: 11
- 资源: 919
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功