jeudi 7 juillet 2016

something wrong when navigating from fragment to fragment

I have one activity and 3 fragments A,B,C when app starts it holds main Fragment A then from Fragment A i'm navigating to Fragment B and from Fragment B to Fragment C. Code works and it opens Fragment C from Fragment B, but first it opens Fragment A and just then Fragment C. Why and how can I fix this? Do I need one more activity to host Fragments B,C?

Method which switches fragments:

    private Fragment contentFragment;
     public void switchContent(Fragment fragment, String tag){
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        while (fragmentManager.popBackStackImmediate());
        if (fragment != null){
            FragmentTransaction transaction = fragmentManager
                    .beginTransaction();
            transaction.replace(R.id.content_frame,fragment,tag);
            if (!(fragment instanceof FragmentB)){
                transaction.addToBackStack(tag);
            }
            transaction.addToBackStack(tag);
            transaction.commit();
            contentFragment = fragment;
        }
    }

R.id.content_frame is id of Layout which is used on activity setContentView.

Aucun commentaire:

Enregistrer un commentaire