Search code examples
androidfragment-tab-host

how to return to fragment in android fragmentTabHost


I have paint this problem:

enter image description here

Do anyone know solution?


Solution

  • Start ActivityB from ActivityA using startActivityForResult.

    int REQUEST_CODE = 99; // random number here
    startActivityForResult(activityAIntent, REQUEST_CODE);
    

    1) In ActivityB call:

      setResult(RESULT_OK);
      finish();
    

    2) In ActivityA catch RESULT_OK from ActivityB:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REQUEST_CODE) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
               // finish ActivityA and navigate back to FragmentA
               setResult(RESULT_OK);
               finish();
            }
        }
    }
    

    Other solution if you don't need ActivityA after you start ActivityB, then call finish() in ActivityA where you start ActivityB. Having that you can simply call finish() in ActivityB and user will be navigated back to FragmentA as expected.