Android oncreateview调用了两次

前端之家收集整理的这篇文章主要介绍了Android oncreateview调用了两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经不止一次地看到这个问题,但是我无法弄明白如何解决我的问题.

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

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 – FragmentA,FragmentB和FragmentC具有几乎相同的代码

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,Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.details_fragment,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上选择了第三个标签.如果我旋转屏幕,我会有以下事件序列:

> ControlPanelFragment onSaveInstanceState保存当前选项卡和当前片段
> FragmentA onSaveInstanceState保存寻呼机的选项卡片段
> navigationDrawer第二个选项再次被称为fragment = ControlPanelFragment.newInstance();
>调用onConCreated的ControlPanelFragment,我可以获取保存的数据信息,并创建新的寻呼机和标签
>调用FragmentA onViewCreated,我可以提取保存的数据
> FragmentADetail onActivityCreated获取保存的数据并正确加载数据(至少我认为)
从现在起第二次调用第二组方法,之前保存的数据被重置,因此它现在显示错误的数据
> ControlPanelFragment onSaveInstanceState但现在savedInstanceState为null
> ControlPanelPagerAdapter getItem被调用实例化3个片段
> FragmentA onSaveInstanceState现在调用它,但savedInstanceState为null
>调用FragmentADetail onActivityCreated但现在tab = 0

有人可以解释一下 – 我怎么能阻止步骤7到10发生?

@H_404_45@

解决方法

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

当我这样做时:

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();
        }
}
@H_404_45@ @H_404_45@ 原文链接:https://www.f2er.com/android/315473.html

猜你在找的Android相关文章