Activity的两种启动模式:FLAG_ACT…

本文详细介绍了Android中Activity的两种启动模式:FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT。通过具体实例展示了如何使用这两种模式来调整应用程序中Activity的堆栈顺序。

Activity的两种启动模式:FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT

 

1. 如果已经启动了四个Activity:A,B,C和D。在D Activity里,我们要跳到B Activity,同时希望C finish掉,可以在startActivity(intent)里的intent里添加flags标记,如下所示:

Java代码  收藏代码
  1. Intent intent new Intent(this, B.class);   
  2. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  3. startActivity(intent);  

 

  这样启动B Activity,就会把D,C都finished掉,如果你的B Activity的启动模式是默认的(multiple 则B Activity会finished掉,再启动一个新的Activity B。

  如果不想重新再创建一个新的B Activity,则在上面的代码里再加上:

Java代码  
  1. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);   

 这样B Activity就不会再创建一个新的了,而是会重用之前的B Activity,同时调用B Activity的onNewIntent()方法。

 

2. 如果已经启动了四个Activity:A,B,C和D,在D Activity里,想再启动一个Actvity B,但不变成A,B,C,D,B,而是希望是A,C,D,B,则可以像下面写代码:

Java代码  
  1. Intent intent new Intent(this, MainActivity.class);  
  2. intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);   
  3. startActivity(intent);  

http://www.cnblogs.com/-OYK/archive/2011/6/30.html

Background activity launch blocked! goo.gle/android-bal [callingPackage: com.google.android.dialer; callingPackageTargetSdk: 35; callingUid: 10135; callingPid: -1; appSwitchState: 2; callingUidHasVisibleActivity: false; callingUidHasNonAppVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; allowBalExemptionForSystemProcess: true; intent: Intent { act=com.android.dialer.incall.statusbarnotification.buttonintentANSWER flg=0x10040000 xflg=0x4 cmp=com.google.android.dialer/com.android.dialer.incall.activity.ui.InCallActivity (has extras) }; callerApp: null; balAllowedByPiCreator: BSP.NONE; resultIfPiCreatorAllowsBal: BAL_ALLOW_SAW_PERMISSION; callerStartMode: MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; hasRealCaller: true; isCallForResult: false; isPendingIntent: true; autoOptInReason: null; realCallingPackage: com.android.systemui; realCallingPackageTargetSdk: 36; realCallingUid: 10087; realCallingPid: 1612; realCallingUidHasVisibleActivity: false; realCallingUidHasNonAppVisibleWindow: true; realCallingUidProcState: PERSISTENT; isRealCallingUidPersistentSystemProcess: true; originatingPendingIntent: PendingIntentRecord{4ea7d9 com.google.android.dialer startActivity (allowlist: 5e9a714:+30s0ms/0/NOTIFICATION_SERVICE/NotificationManagerService)}; realCallerApp: ProcessRecord{864d437 1612:com.android.systemui/u0a87}; realInVisibleTask: false; balAllowedByPiSender: BSP.ALLOW_FGS; resultIfPiSenderAllowsBal: BAL_ALLOW_NON_APP_VISIBLE_WINDOW; realCallerStartMode: MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; balRequireOptInByPendingIntentCreator: true; balDontBringExistingBackgroundTaskStackToFg: true]
最新发布
09-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值