0

There are 3 activities A, B, C.

A -> B -> C with button

A <------ C with button that close C and B?

A -> B

   @Override
   public void onClick(View v) {
       Intent intent = new Intent(A.this, B.class);
       startActivity(intent);
   }

B -> C

   @Override
   public void onClick(View v) {
       Intent intent = new Intent(B.this, C.class);
       startActivity(intent);
   }

B -> A

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

C -> B

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

How to back to A from C with button that close C and B?

Tutuy
  • 11
  • 3
  • 1
    Check https://stackoverflow.com/questions/32198055/how-to-finish-multiple-activities-at-once-in-android. You can also use `startActivityForResult` for this case just send result back to Activity B from Activity C and it will close and now you will be On Activity A . You can Also use `Intent.FLAG_ACTIVITY_CLEAR_TOP` [Like This](https://stackoverflow.com/questions/6330260/finish-all-previous-activities). – ADM Aug 27 '21 at 05:05
  • ever considered using fragments instead of activities? – Roshana Pitigala Aug 27 '21 at 05:20
  • ```Intent.FLAG_ACTIVITY_CLEAR_TOP``` didn't work – Tutuy Aug 27 '21 at 06:40

2 Answers2

0

Simply use finish() for this.

B -> C

@Override
public void onClick(View v) {

    Intent intent = new Intent(B.this, C.class);

    startActivity(intent);
    
    finish(); //Solution
}

Note: You can also use launch modes.

0

Use startActivityForResult for A -> B and B -> C then override below method in C

override fun onBackPressed() {
    setResult(RESULT_OK)
    super.onBackPressed()
}

now in B override below method

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_CANCELED) return

        when (requestCode) {
            YoutIntentCodeWhenYouGoToC -> {
                setResult(RESULT_OK)
                onBackPressed()
            }
        }
    }

Now you are in A. And if you press a back button then it wont show B or C screen.

Hardik Hirpara
  • 2,594
  • 20
  • 34