意图setFlags FLAG_ACTIVITY_CLEAR_TOP意图、setFlags、FLAG_ACTIVITY_CLEAR_TOP

2023-09-04 07:34:40 作者:梦想,理应飞翔。

在Android上,我开始4活动A,B,C,D,如果我想去从D-回到A,我可以用'intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)。但是,如果活动如下打开同一类的活动,我怎么能回去,从数字到现在?

 意向书我=新的意图(FlagsTest.this,FlagsTest.class);
startActivity(ⅰ);
 

解决方案

试试这个办法,希望这将帮助你解决你的问题。

1.write这$ C $对主人的活动或应用程序的活动ç

 私人KillReceiver clearActivityStack;

 @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    clearActivityStack =新KillReceiver();
    registerReceiver(clearActivityStack,IntentFilter.create(clearStackActivity,text / plain的));
   //注册以清除栈
}

私人final类KillReceiver扩展的BroadcastReceiver {
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        完();
    }
}

@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    unregisterReceiver(clearActivityStack); //注销,以明确的协议栈
}
 

2.记本code之前启动A活动

 意向意图=新的意图(clearStackActivity);
 intent.setType(text / plain的);
 sendBroadcast(意向);
 
面试官装逼失败之 Activity的启动模式

3.启动你的活动

 意向意图=新的意图(这一点,类);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(意向);
 

On Android, I started 4 activities A, B, C, D, if I want to go back from D to A, I can use 'intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)'. But if the activities are same class activities opened as follows, How can I go back from D to A now?

Intent i = new Intent(FlagsTest.this, FlagsTest.class);
startActivity(i);

解决方案

Try This way,hope this will help you to solve your problem.

1.write this code on master activity or application activity.

private KillReceiver clearActivityStack;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    clearActivityStack = new KillReceiver();
    registerReceiver(clearActivityStack, IntentFilter.create("clearStackActivity", "text/plain"));
   // register to clear stack
}

private final class KillReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        finish();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(clearActivityStack); // unregister to clear stack
}

2.write this code before start A Activity

 Intent intent = new Intent("clearStackActivity");
 intent.setType("text/plain");
 sendBroadcast(intent);

3.start your activity

Intent intent = new Intent(this, Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);