Nested fragments - Screen of Frag2 stays empty

女生的网名这么多〃 提交于 2019-12-02 04:09:14

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.

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();

use content_frame id of activity_main in Frag1 class

back1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("frag1", "createdview");
            //getActivity().getFragmentManager().popBackStackImmediate();
            rl1.setVisibility(View.INVISIBLE);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            Frag2 f2 = new Frag2();
            ft.replace(R.id.content_frame, f2);
            ft.addToBackStack(null);
            ft.commit();
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!