getSupportFragmentManager

getSupportFragmentManager

getSupportFragmentManager在片段内部不起作用

Fragment tFragment = MainActivity.newInstance();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.frame_container, tFragment);
        ft.commit();


任何答案都是可观的

最佳答案

private FragmentActivity myContext;


覆盖片段的onAttach方法:

@Override
public void onAttach(Activity activity) {
    myContext=(FragmentActivity) activity;
    super.onAttach(activity);
}


当您需要获取支持片段管理器时,请致电:

FragmentManager fragManager = myContext.getSupportFragmentManager();


要么

FragmentManager fragManager = getActivity().getSupportFragmentManager();


见下面的链接:

How can I access getSupportFragmentManager() in a fragment?

关于android - 如何从 fragment 获取getSupportFragmentManager(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24282292/

10-10 19:52