为什么有时候启动activity flag 详解需要加FLAG

在BroadcastReceiver中启动Activity的问题
* Demo描述:
* 在BroadcastReceiver中启动Activity的问题
* 如果在BroadcastReceiver的onReceive()方法中如下启动一个Activity
* Intent intent=new Intent(context,AnotherActivity.class);
* context.startActivity(intent);
* 可捕获异常信息:
* android.util.RuntimeException:
* Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
* Is this really what you want?
* 它说明:在Activity的context(上下文环境)之外调用startActivity()方法时
* 需要给Intent设置一个flag:FLAG_ACTIVITY_NEW_TASK
* 所以在BroadcastReceiver的onReceive()方法中启动Activity应写为:
* Intent intent=new Intent(context,AnotherActivity.class);
* intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
* context.startActivity(intent);
* 之前描述了问题的现象和解决办法,现在试着解释一下原因:
* 1 在普通情况下,必须要有前一个Activity的Context,才能启动后一个Activity
* 2 但是在BroadcastReceiver里面是没有Activity的Context的
* 3 对于startActivity()方法,中有这么一段描述:
Note that if this method is being called from outside of an
{@link android.app.Activity} Context, then the Intent must include
the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag.
This is because,
without being started from an existing Activity, there is no existing
task in which to place the new activity and thus it needs to be placed
in its own separate task.
说白了就是如果不加这个flag就没有一个Task来存放新启动的Activity.
* 4 其实该flag和设置Activity的LaunchMode为SingleTask的效果是一样的
* 如有更加深入的理解,请指点,多谢
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'laokaddk 的BLOG
用户名:laokaddk
文章数:983
评论数:106
访问量:2418962
注册日期:
阅读量:5863
阅读量:12276
阅读量:394928
阅读量:1085807
51CTO推荐博文
[plain]ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities) & & &Main stack: & & & &TaskRecord{ A com.leaves.ipanel U 0} & & & &Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x cmp=com.leaves.ipanel/.MainActivity } & & & & &Hist #3: ActivityRecord{4140fdf8 u0 com.leaves.ipanel/.ActivityB} & & & & & &Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB } & & & & & &ProcessRecord{50:com.leaves.ipanel/u0a10061} & & & & &Hist #2: ActivityRecord{413ff7e8 u0 com.leaves.ipanel/.ActivityA} & & & & & &Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA } & & & & & &ProcessRecord{50:com.leaves.ipanel/u0a10061} & & & & &Hist #1: ActivityRecord{413f8b28 u0 com.leaves.ipanel/.MainActivity} & & & & & &Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x cmp=com.leaves.ipanel/.MainActivity } & & & & & &ProcessRecord{50:com.leaves.ipanel/u0a10061} & & & &TaskRecord{ A com.android.launcher U 0} & & & &Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x cmp=com.android.launcher/com.android.launcher2.Launcher } & & & & &Hist #0: ActivityRecord{ com.android.launcher/com.android.launcher2.Launcher} & & & & & &Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x cmp=com.android.launcher/com.android.launcher2.Launcher } & & & & & &ProcessRecord{:com.android.launcher/1000} & &可以看到三者在同一个task中,且都存在。接下来我们在启动ActivityB时添加FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK[java]publicvoid onClick(View v) { & & & &Log.i(TAG, "--onClick--task id = " + getCurrentTaskId()); & & &
& &Intent intent = new Intent("com.leaves.ipanel.ActivityB"); & & & & &intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); & & & &startActivity(intent); & & } & &再看下启动ActivityB之后的堆栈:[plain]ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities) & & &Main stack: & & & &TaskRecord{ A com.leaves.ipanel U 0} & & & &Intent { act=com.leaves.ipanel.ActivityB flg=0x cmp=com.leaves.ipanel/.ActivityB } & & & & &Hist #1: ActivityRecord{413f8b28 u0 com.leaves.ipanel/.ActivityB} & & & & & &Intent { act=com.leaves.ipanel.ActivityB flg=0x cmp=com.leaves.ipanel/.ActivityB } & & & & & &ProcessRecord{41:com.leaves.ipanel/u0a10061} & & & &TaskRecord{ A com.android.launcher U 0} & & & &Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x cmp=com.android.launcher/com.android.launcher2.Launcher } & & & & &Hist #0: ActivityRecord{ com.android.launcher/com.android.launcher2.Launcher} & & & & & &Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x cmp=com.android.launcher/com.android.launcher2.Launcher } & & & & & &ProcessRecord{:com.android.launcher/1000} & &可以看到,MainActivity和ActivityA都被结束掉了,原因就是因为该task用来装载ActivityB,而启动时又设置了FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK
了这篇文章
类别:┆阅读(0)┆评论(0)}

我要回帖

更多关于 activity四种启动模式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信