android how to finish an activity from other activity

just finish the second activity when you open third activity

suppose in second activity on some button click you are opening third activity using start activity;

startActivity(intent);
finish();//this will finish second activity and open third activity so when you press back from third activity it will open first activity.

if you want depended on some condition then on activity

setResult(123);

something code like this

now when override onActivityResult in second activity

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==123){
            //finish
        }
    }

also make sure one thing you need to use startActivityForResult(intent, requestCode); for result in second activity to start third activity.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity2 extends Activity{

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(new Intent(Activity2.this,Activity3.class)), 12);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode==123 && requestCode==12){
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity3 extends Activity{

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                setResult(123);
            }
        });
    }
}

When you start your third activity you should call in your 2nd acitivity like this:

startActivityForResult(intent, 0)

When you finish your 3rd activity, you should include:

setResult(RESULT_OK);
finish();

It sends a signal from the 3rd activity to the 2nd.

In the 2nd put:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     finish();
}

It receives this signal and finishes the 2nd activity.


You should launch the 1st activity in 3rd activity with the cleartop flag when you need to finish the 2nd activity.

class ThirdActivity extends Activity {
    ....
    if (somecondition) { 
         /* directly go to FirstActivity, finish all intermediate ones.*/
        Intent intent = new Intent(this, FirstActivity.Class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        finish(); // to simply return to 2nd activity.
    }
    .....
    ....
 }

Alternatively, you could set an intent flag called FLAG_ACTIVITY_NO_HISTORY on the 2nd Activity. This way it is not kept in the history stack so when you navigate back from the 3rd activity it will go straight to the 1st.

E.g.

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

This means that when you launch an activity from the SecondActivity the SecondActivity will be finished automatically.