0

I have 2 activities. Activity - A and B

From Activity-A I go to Activity-B through a button press and launching an Intent.

Then after some action at Activity-B, I go back to Activity-A using Intent like below.

public class ActivityB extends AppCompatActivity {

    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Intent failIntent = new Intent(getBaseContext(), ActivityA.class);
                    startActivity(failIntent);
                }catch (Exception e) {

                }
            }
        });
    }
}

Whenever we launch an activity with startActivity (Intent), it launches a new instance of the activity. How can we make sure we launch a previous instance of the activity? In this case a previous instance of Activity A?

nad
  • 2,640
  • 11
  • 55
  • 96

1 Answers1

1

using finish() works

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
nad
  • 2,640
  • 11
  • 55
  • 96