Search code examples
android-tabhost

Backpress to previous fragment in another activity


It seems each activity has its own Fragment Manager but I have a situation when I need to access the Fragment Manager of a previous activity. Here is the situation. I have tabs A - E launching Activities A - E in each tab. Each activity have fragments. So I am in Fragment B1 (in Activity B launched by Fragment A3 of Activity A) and I want to go back to A3 but since A3 is in another Activity, I can't use popBackStack on BackPress because Activity B doesn't know (can't access) the BackStack of Activity A. How do I get back to A3 of Activity A from B1 of Activity B. Thanks


Solution

  • See when one activity calls another activity, the caller activity's instance state is saved in stack so if you want to go back from called activity to the caller fragment try calling finish(); in the back pressed method and it will do the trick for you.eg

    @Override
    onBackPressed{finish();}