Android oncreateview调用了两次

时间:2014-12-31 16:53:08

标签: android android-fragments android-viewpager

我已经不止一次地在这里问过这个问题,但我无法弄清楚如何解决我的问题。

我有一个用户执行此操作的应用程序:

1 - 打开navigationDrawer并选择一个选项(创建一个片段)(我在这里选择第二个选项);

public void selectItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case FRAGMENT_OPTION1:
                ...
                break;
            case FRAGMENT_OPTION2:
                fragment = ControlPanelFragment.newInstance();
                break;
            ...
            case FRAGMENT_OPTIONN:
                ...
                return;
            default:
                break;
        }
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
        }
}

2 - 加载所选选项(ControlPanelFragment):     2.1 - 控制面板有标签和iconpager。对于每个寻呼机页面和每个选项卡,都会创建一个新的片段。我有3个标签和3个页面,因此创建了9个片段;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (savedInstanceState != null) {
        currentControlPanelOption = savedInstanceState.getInt("currentControlPanelOption", currentControlPanelOption);
        currentControlPanelTab = savedInstanceState.getInt("currentControlPanelTab", currentControlPanelTab);
    }
    setControlPanelTabs();
    setIconPager();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("currentControlPanelOption", pager.getCurrentItem());
    outState.putInt("currentControlPanelTab", mTabHost.getCurrentTab());
}

3 - 在setIconPager();我有这段代码:

pager = (ViewPager) view.findViewById(R.id.pager);
cPanelPagerAdapter = new ControlPanelPagerAdapter(getChildFragmentManager());
pager.setOffscreenPageLimit(2);
pager.setAdapter(cPanelPagerAdapter);

其中ControlPanelPagerAdapter包含此代码:

public Fragment getItem(int index) {
    Fragment fragment;
    switch (index) {
        case 1:
            fragment = FragmentA.newInstance();
            break;
        case 2:
            fragment = FragmentB.newInstance();
            break;
        case 3:
            fragment = FragmentC.newInstance();
            break;
        default:
            fragment = null;
            break;
    }
    ...
    return fragment;
}

4 - FragmentAFragmentBFragmentC的代码几乎相同:

public static FragmentA newInstance() {
    return new FragmentA();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_placeholder, container, false);
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (savedInstanceState == null) {
        fragmentA_Data = new FragmentADetail[3];
        createTabInstance(0);
    } else {
        fragmentA_Data = (FragmentADetail[]) savedInstanceState.getSerializable("Data");
        return;
    }
}
private void createTabInstance(int tab) {
    new FragmentADetail();
    fragment = FragmentADetail.newInstance(tab);

    Bundle args = new Bundle();
    args.putInt("tab", tab);
    fragment.setArguments(args);

    fragmentA_Data[tab] = fragment;
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_placeholder, fragmentA_Data[tab]);
    fragmentTransaction.commitAllowingStateLoss();
}
public void getTabData(int tab) {
    if (fragmentA_Data[tab] == null) {
        createStoreTimePeriodInstance(tab);
    } else {
        if (fragmentA_Data[tab].getArguments() == null) {
            Bundle args = new Bundle();
            args.putInt("tab", tab);
            fragmentA_Data[tab].setArguments(args);
        }
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_placeholder, fragmentA_Data[tab]);
        fragmentTransaction.commitAllowingStateLoss();
    }
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("data", fragmentA_Data);
}

5 - 最后,FragmentADetail有以下代码:

public static FragmentADetail newInstance(int tab) {
    selectedTab = tab;
    return new FragmentADetail();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.details_fragment, container, false);
    ...
    if (savedInstanceState != null) {
        selectedTab = savedInstanceState.getInt("selectedTab");
    }
    ...
}

public void getTabData(int tab) {
    //This is where I'm getting the data that populates the layout
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("selectedTab", selectedTab);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Bundle args = getArguments();
    if (args != null) {
       getTabData(args.getInt("tab"));
    }
}

现在,想象一下我在FragmentA选择了第三个标签。如果我旋转屏幕,我会有以下事件序列:

  1. ControlPanelFragment onSaveInstanceState保存当前标签和当前片段
  2. FragmentA onSaveInstanceState保存寻呼机的标签片段
  3. navigationDrawer第二个选项再次被称为fragment = ControlPanelFragment.newInstance();
  4. 调用ControlPanelFragment onViewCreated,我可以获取已保存的数据信息,并创建新的寻呼机和标签
  5. 调用FragmentA onViewCreated,我可以提取已保存的数据
  6. FragmentADetail onActivityCreated获取已保存的数据并正确加载数据(至少我认为) 从现在起第二次调用第二组方法,之前保存的数据被重置,因此现在显示错误的数据
  7. ControlPanelFragment onSaveInstanceState但现在savedInstanceState为空
  8. ControlPanelPagerAdapter getItem被称为实例化3个片段
  9. FragmentA onSaveInstanceState现在已调用但savedInstanceState为null
  10. 调用FragmentADetail onActivityCreated,但现在tab = 0
  11. 有人可以解释一下我怎样才能阻止第7到10步发生?

1 个答案:

答案 0 :(得分:12)

我弄清楚我的问题是什么。

当我这样做时:

case FRAGMENT_OPTION2:
    fragment = ControlPanelFragment.newInstance();
break;

我正在创建一个片段,当我旋转屏幕时selectItem(int position)再次被调用,因此创建了同一个对象的新实例,因此步骤7及其后续步骤。解决方案是检查片段是否已经创建并使用他而不是创建新片段。 我用标签保存了初始片段,然后查找了该标签。如果标签存在,请使用该片段,否则创建一个新片段。

public void selectItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case FRAGMENT_OPTION1:
                ...
                break;
            case FRAGMENT_OPTION2:
                fragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(position));
                if (fragment == null) {
                    fragment = ControlPanelFragment.newInstance();
                }
                break;
            ...
            case FRAGMENT_OPTIONN:
                ...
                return;
            default:
                break;
        }
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment, 
                    String.valueOf(position)).commitAllowingStateLoss();
        }
}