Finish an activity from another activity

how to finish activity from another class in android
how to destroy an activity from another activity in android
android get activity back stack
flag activity clear top
finish previous two activity android
android go back to previous activity without finish
flag_activity_new_task example
android clear back stack

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();
  1. Make your activity A in manifest file: launchMode = "singleInstance"

  2. When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.

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

Killing one activity from another, I found a nice way to finish one activity from another, it is similar to what Lumis did​. So if you want to close ActivityA from ActivityB you can do this:. I have 3 activities A, B and C. A leads to B which leads to C. I would like to be able to move back and forth between A and B but I want to finish both A and B once C gets started. I understand how

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

Android process death, How do I go back to previous activity on android? How to finish one activity from another activity in android Create New project in android studio. Refer Android beginners app development guide New two activity’s. After creating the project a new activity will be automatically created Implement the broadcast code in current activity. Code

This is a fairly standard communication question. One approach would be to use a ResultReceiver in Activity A:

Intent GotoB=new Intent(A.this,B.class);
GotoB.putExtra("finisher", new ResultReceiver(null) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        A.this.finish();
    }
});
startActivityForResult(GotoB,1);

and then in Activity B you can just finish it on demand like so:

((ResultReceiver)getIntent().getExtra("finisher")).send(1, new Bundle());

Try something like that.

How to close activity and go back to previous activity in android , In android there are different ways to finish one activity from another activity ,one of them is using startActivityForResult intent ,another one is using interface  Finish an activity from another activity (7) 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.

There is one approach that you can use in your case.

Step1: Start Activity B from Activity A

startActivity(new Intent(A.this, B.class));

Step2: If the user clicks on modify button start Activity A using the FLAG_ACTIVITY_CLEAR_TOP.Also, pass the flag in extra.

Intent i = new Intent(B.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("flag", "modify");
startActivity(i);
finish();

Step3: If the user clicks on Add button start Activity A using the FLAG_ACTIVITY_CLEAR_TOP.Also, pass the flag in extra. FLAG_ACTIVITY_CLEAR_TOP will clear all the opened activities up to the target and restart if no launch mode is defined in the target activity

Intent i = new Intent(B.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("flag", "add");
startActivity(i);
finish();

Step4: Now onCreate() method of the Activity A, need to retrieve that flag.

String flag = getIntent().getStringExtra("flag");
if(flag.equals("add")) {
    //Write a code for add
}else {
    //Write a code for modifying
}

Access the Current Android Activity from Anywhere!, If the user presses the Back button, that new activity is finished and has simply lost focus while another task takes place, as shown in figure 2. I want to finish an activity from my class which is extends PhoneStateListener class. (I know how to finish an activity from another, but i unable to do the same from a class file.) I searched a lot on internet,but no luck. Please help me on this

See my answer to Stack Overflow question Finish All previous activities.

What you need is to add the Intent.FLAG_CLEAR_TOP. This flag makes sure that all activities above the targeted activity in the stack are finished and that one is shown.

Another thing that you need is the SINGLE_TOP flag. With this one you prevent Android from creating a new activity if there is one already created in the stack.

Just be wary that if the activity was already created, the intent with these flags will be delivered in the method called onNewIntent(intent) (you need to overload it to handle it) in the target activity.

Then in onNewIntent you have a method called restart or something that will call finish() and launch a new intent toward itself, or have a repopulate() method that will set the new data. I prefer the second approach, it is less expensive and you can always extract the onCreate logic into a separate method that you can call for populate.

How to finish one activity from another activity in android – Codesfor, Particularly in activity-centric UI architectures, there may be cases where cases where we need to call finish() without starting another activity. In this video there are 2 activities, let's call them in alphabetical order. A -> B, A starts the second Activity(B), B calls finish(). When finish() is called, it would go back to the previous activity again (A) and start Activity A.

Understand Tasks and Back Stack, To run our application you should enter your new activity in AndroidManifest. another one way is, just when come from MainActivitySecond class, just add the  I found a nice way to finish one activity from another, it is similar to what Lumis did. So if you want to close ActivityA from ActivityB you can do this: In ActivityA do: className = this.getClass().getName(); and pass it on to AvtivityB. Then in ActivityB do: ((Activity) Class.forName(className).newInstance()).finish();

Activity, In this video there are 2 activities, let's call them in alphabetical order. A -> B,. A starts the second Activity(B), B calls finish(). When finish() is  If you used Eclipse to create this activity: The class already includes an implementation of the required onCreate () method. There's also an implementation of the onCreateOptionsMenu () method, but you won't need it for this app so you can remove it. There's also an implementation of onOptionsItemSelected ()

Start Before You Finish, Start a new project in Android Studio using an Empty Activity. Hit Finish and you should see two new files created — a… Let's add another activity. Find 1,436 synonyms for activities and other similar words that you can use instead based on 6 separate contexts from our thesaurus. What's another word for What's the opposite of

Comments
  • hey, you wanted to finish one activity from another right... then I had posted the answer which is correct. But, why did you vote it down ?
  • There is a thread with a few nice answers here: stackoverflow.com/questions/14355731/…
  • Great answer..!!
  • You are holding a static reference to an activity. This will never go away unless you clear it manually from somewhere -> you are creating a memory leak!
  • Well its ok, you have my +1 for out-of-the-box thinking :D, i would just change to singleTop. however it still won't be my first choice when doing something similar. by cleanup code i meant a way to get rid of the static reference so there is no memory leaking, but that depends on the app structure.
  • Please no one use the first solution, it is completely wrong. You are bypassing the Android framework in a really hacky way whilst creating a zombie object. Use a BroadcastReceiver.
  • Awesome solutions. Worked very well for my problem in which I was going from activities A->B->C and on onBackPressed of C, I was launching new instance of B. But then if I did onBackPressed on that new instance of B, it used to go to the old instance of B instead of old instance of A (what I wanted). So I followed your first method in the onBackPressed of C, and it gave me the behaviour I wanted :) Thanks a ton :)
  • It shows this error "Syntax error on tokens, AnnotationName expected instead" on "registerReceiver(broadcast_reciever, new IntentFilter("finish_activity"));". What's wrong?
  • "you should not break the normal flow of activity" +1 for this
  • Don't forget to unregister broadcast_receiver with unregisterReceiver()
  • Obviously not working if you turn Dev Options->Don't Keep Activities because this solution break normal flow
  • This is the solution. nice one
  • +1 just , ((ResultReceiver)getIntent().getParcelableExtra("finisher")).send(1, new Bundle());