0

I have a fragment added statically from XML I want to replace this fragment by another fragment, I did that by adding this code:

        CFragment singleStationFragment = new CFragment();
        android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.layoutlist, singleStationFragment);
        transaction.addToBackStack(null);
        transaction.commit();

the problem is that when I press the back button the first fragment is not shown because it was not added through a transaction and the manager doesn't know about it, is there a way I could add the first fragment (ALREADY ADDED FROM XML), to my backstack or I could just show it when I click back instead of exiting the app ? Thanks !

4

1 回答 1

0

据我所知,您必须将第一个片段添加到代码中的活动而不是布局文件中。add使用以下方法执行此操作FragmentTransaction

transaction.add(R.id.FragmentContainer, fragment);
于 2014-01-13T16:53:26.890 回答