2

I have a program where when one user clicks on a button, it takes them from one fragment to another. The issues is that I want the previous fragment to reappear once the click the back button. Code below:

This is creating the first fragment:

getFragmentManager().beginTransaction().add(R.id.team_fragment_holder, teamFragment).addToBackStack(null).commit();

And this is switching to the second fragment:

setContentView(R.layout.mixed_team_holder);
    FragmentManager fragMan = getFragmentManager();
    FragmentTransaction fragTrans = fragMan.beginTransaction();
    fragTrans.add(R.id.team_1,team1);
    fragTrans.add(R.id.team_2, team2);
    fragTrans.commit();

The problem right now is that when i hit the back button, a blank page appears (i assume is the (R.id.team_fragment_holder). Any idea to make the first fragment reappear along with all the information on it?

Note: all fragments extends listfragment.

Thanks in advance.

3 Answers 3

4

Your initial fragment transaction should not be added to the backstack and the second one should. The blank page you see when pressing back is the result of removing the teamFragment because it is on the backstack.

getFragmentManager().beginTransaction().add(R.id.team_fragment_holder, teamFragment).commit();

and

FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.add(R.id.team_1,team1);
fragTrans.add(R.id.team_2, team2);
fragTrans.addToBackStack(null);
fragTrans.commit();
3
  • Thanks of the response but it didn't work. After i click the back button, I still get a blank page. Do I have to save everything from the previous fragment before i create the next one and reload it back in after the back button is pressed?
    – Quinn Wei
    May 22, 2014 at 0:42
  • Or maybe the blank screen resulted because I set a new layout to display the team1 and team2 fragments?
    – Quinn Wei
    May 22, 2014 at 0:57
  • It's probably because you set a new layout. You shouldn't have to re-set your layout like that. It can/should all be handled with Fragments programmatically. May 22, 2014 at 3:39
1

You need add the fragment to Back Stack.

FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.add(R.id.team_1,team1);
fragTrans.add(R.id.team_2, team2);
fragTrans.addToBackStack(null);//Here add the fragment
fragTrans.commit();

And you need validate the back button.

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
     } else {
        getSupportFragmentManager().popBackStack();
     }
}
2
  • Activity already handles popping the fragment backstack for you. There's no need to override onBackPressed(). May 21, 2014 at 23:53
  • Same Problem as the first answer: a blank page is displayed after i press the back button. Just wondering if i need to save everything on the first fragment before calling the second one and reload it afterwards. Thanks!
    – Quinn Wei
    May 22, 2014 at 0:46
1
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.fragment_button_project_location_ok:

        //getActivity().finish();
        //add back to previous screen
        getFragmentManager().popBackStack();
        break;

    default:
        break;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.