How to destroy previous activity in Activity?

夙愿已清 提交于 2019-12-23 02:54:07

问题


I have four activity, i.e. A, B, C and D. A launches B, B launches C, C launches D. When C is launching D, I want to destroy the activity B depending on the situation(logic for that will remain in activity C) so that when I go back From D, it will follow D->C->A path. So I want to destroy activity B from C. How it is possible?


回答1:


finish Activity B when you are calling Activity C depends on your logic. For example

if(true){
Intent in = new Intent(B.this,c.class);
startActivity(c);
}
else
{
Intent in = new Intent(B.this,c.class);
startActivity(c);
finish();
}



回答2:


finishActivity(requestCode);

this method may help you..

What to do is start the activity C with the some request code from B... and accordingly finish the activity with this request code




回答3:


Simply call finish(); in activity B's onActivityResult when returning from C depending on the logic you want




回答4:


I think what you can do is that you can register a broadcast in each class and whenever you want to finish sendbroadcast and finish that activity.

        // REGISTER IN ONCREATE
        BroadcastReceiver form_filled = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String received_action = intent.getAction();

                if (received_action.equals("finish_a")) {
                    finish();
                }
            }
        };
        registerReceiver(form_filled, new IntentFilter("finish_a"));

        // THIS YOU HAVE TO DO WHEN YOU WANT TO FINISH
        Intent temp_intent = new Intent();
        temp_intent.setAction("finish_a");
        sendBroadcast(temp_intent);



回答5:


Ok then you can call startActivityForResult(in,5); to start Activity C. and implement the override method in Activity B like

 @Override 
   protected void onActivityResult(int requestCode, int resultCode, Intent intent)       {                
super.onActivityResult(requestCode, resultCode, intent); 
    if(resultCode==0)
    { 
      finish(); 
    }
    else
   {

    } 
    }

And set the resultcode in Activity when you are calling Activity D like

Intent backintent = getIntent(); 
  setResult(0); 
  Intent in = new Intent(C.this,D.class); 
  startActivity(in);

Thats it....



来源:https://stackoverflow.com/questions/10479965/how-to-destroy-previous-activity-in-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!