I have 4 fragment in my android app : let's say A B C D where they 4 are fragments called in 1 activity. When I go from A to B and from be to C I want the back button to bring me to B and to A if I press again. But instead the back button forwards me to the previous activity.
I've read that the back button need to explicitely be handled as for fragments and I saw that code :
FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();
fragment.getView().setFocusableInTouchMode(true);
fragment.getView().setOnKeyListener( new OnKeyListener()
{
@Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
    if( keyCode == KeyEvent.KEYCODE_BACK )
    {
        return true;
    }
    return false;
    }
} );
The problem is that I'm not using any FragmentTransaction. Here is how I call my Fragments :
private void displayView(int position) {
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    /*case 1:
        fragment = new ContactsFragment();
        break;*/
    case 2:
        fragment = new ContactsFragment();
        break;
    case 3:
        fragment = new SitesFragment();
        break;
    default:
        break;
    }
    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
}
How can I use that back stacking with my code please ? Thank you for your help in advance !
 
     
     
    