I want to finish one activity from another activity, like:
In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A].
Now in Activity [B], there are two buttons, New and Modify. When the user clicks on modify then pop an activity [A] from the stack with all the options ticked..
But when the user click on New button from Activity [B], then I will have to finish Activity [A] from the stack and reload that Activity [A] again into the stack.
I am trying it, but I am not able to finish Activity [A] from the stack... How can I do it?
I am using the code as:
From Activity [A]:
Intent GotoB = new Intent(A.this,B.class); startActivityForResult(GotoB,1);
Another method in same activity
public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == 1) { if (resultCode == 1) { Intent i = getIntent(); overridePendingTransition(0, 0); i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(i); } } }
And in Activity [B], on button click:
setResult(1); finish();
In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A]. Now in Activity [B], there are two buttons, New and Modify. When the user clicks on modify then pop an activity [A] from the stack with all the options ticked..
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
Make your activity A in manifest file: launchMode = "singleInstance"
When the user clicks new, do FirstActivity.fa.finish();
and call the new Intent.
When the user clicks modify, call the new Intent or simply finish activity B.
FIRST WAY
In your first activity, declare one Activity object
like this,
public static Activity fa; onCreate() { fa = this; }
now use that object in another Activity
to finish first-activity like this,
onCreate() { FirstActivity.fa.finish(); }
SECOND WAY
While calling your activity FirstActivity
which you want to finish as soon as you move on, You can add flag while calling FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack
if you want to show the FirstActivity
you will have to call it using intent.
That you can do, but I think you should not break the normal flow of activity. If you want to finish you activity then you can simply send a broadcast from your activity B to activity A.
Create a broadcast receiver before starting your activity B:
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent intent) { String action = intent.getAction(); if (action.equals("finish_activity")) { finish(); // DO WHATEVER YOU WANT. } } }; registerReceiver(broadcastReceiver, new IntentFilter("finish_activity"));
Send broadcast from activity B to activity A when you want to finish activity A from B
Intent intent = new Intent("finish_activity"); sendBroadcast(intent);
I hope it will work for you...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With