Android oncreateview调用了两次

Android oncreateview调用了两次,第1张

概述我已经不止一次地看到这个问题,但是我无法弄明白如何解决我的问题. 我有一个用户执行此 *** 作的应用: 1 – 打开navigationDrawer并选择一个选项(创建一个片段)(我在这里选择第二个选项); public void selectItem(int position) { Fragment fragment = null; switch (position) 我已经不止一次地看到这个问题,但是我无法弄明白如何解决我的问题.

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

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个片段;

@OverrIDepublic 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();}@OverrIDepublic 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();}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {    VIEw vIEw = inflater.inflate(R.layout.fragment_placeholder,container,false);    return vIEw;}@OverrIDepublic 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();    }}@OverrIDepublic 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();}@OverrIDepublic 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}@OverrIDepublic voID onSaveInstanceState(Bundle outState) {    super.onSaveInstanceState(outState);    outState.putInt("selectedTab",selectedTab);}@OverrIDepublic 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发生?

解决方法 我弄清楚我的问题是什么.

当我这样做时:

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();        }}
总结

以上是内存溢出为你收集整理的Android oncreateview调用了两次全部内容,希望文章能够帮你解决Android oncreateview调用了两次所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存