Skip to content Skip to sidebar Skip to footer

Nested Fragments - Screen Of Frag2 Stays Empty

I'm suffering through nested fragments. I have a mainactivity which calls a fragment 1 which in turns call a fragment via a button. The fragment frag2 is well instantiated but the

Solution 1:

if your fragment 1 is having another FrameLayout where you are replacing fragment 2, then instead of getFragmentManager(), use getChildFragmentManager() in fragment 1

otherwise you are passing wrong container id in replace() method R.id.fl1 inside fragment1. you should pass R.id.content_frame there.

Solution 2:

You have not posted the fragment1 XML Code. lets assume RelativeLayout (rl1) is your parent layout and you have FrameLayout (fl1) inside the rl1. You are making the parent layout invisible. So the fragment2 can't be visible.

If you don't want to show the fragment1 while showing there is no need for the nested fragment.

You should call like this to load second fragment.

getFragmentManager().beginTransaction()
                .replace(R.id.content_frame ,new Frag2());
                .addToBackStack(null);
                .commit();

Solution 3:

use content_frame id of activity_main in Frag1 class

back1.setOnClickListener(newView.OnClickListener() {
        publicvoidonClick(View v) {
            Log.i("frag1", "createdview");
            //getActivity().getFragmentManager().popBackStackImmediate();
            rl1.setVisibility(View.INVISIBLE);
            FragmentTransactionft= getFragmentManager().beginTransaction();
            Frag2f2=newFrag2();
            ft.replace(R.id.content_frame, f2);
            ft.addToBackStack(null);
            ft.commit();
        }
    });

Post a Comment for "Nested Fragments - Screen Of Frag2 Stays Empty"