popBackStack在单个活动中的三个片段的最佳方式?

时间:2016-08-17 06:34:52

标签: android android-fragments fragment

我得到的问题是,webservice在其他片段的popbackstack中第二次调用onCreateView。

1 个答案:

答案 0 :(得分:1)

我建议您在构建之前检查this documentation

如果您已设法与片段进行通信。然后你可以使用这个简单的方法。

private void changeFragment(Fragment frag, boolean saveInBackstack) {
    String backStateName = ((Object) frag).getClass().getName();

    try {
        FragmentManager manager = getSupportFragmentManager();

        if (manager.findFragmentByTag(backStateName) == null) {
            //fragment not in back stack, create it.
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.container, frag, backStateName);

            if (saveInBackstack) {
                Log.d(TAG, "Change Fragment: addToBackTack " + backStateName);
                transaction.addToBackStack(backStateName);
            } else {
                Log.d(TAG, "Change Fragment: NO addToBackTack");
            }

            transaction.commit();
        } else {
            manager.popBackStack();
        }
    } catch (IllegalStateException exception) {
        Log.w(TAG, "Unable to commit fragment, could be activity as been killed in
    }
}