dev_android
dev_android

Reputation: 8818

How to destroy previous activity in Activity?

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?

Upvotes: 1

Views: 14365

Answers (5)

Raghu Mudem
Raghu Mudem

Reputation: 6963

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....

Upvotes: 0

Raghu Mudem
Raghu Mudem

Reputation: 6963

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();
}

Upvotes: 4

Bharat Sharma
Bharat Sharma

Reputation: 3966

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);

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

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

Upvotes: 1

NullPointerException
NullPointerException

Reputation: 4008

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

Upvotes: 1

Related Questions