gpt4 book ai didi

Android oncreateview 调用了两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:25 26 4
gpt4 key购买 nike

我不止一次看到这个问题在这里被问到,但我不知道如何解决我的问题。

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

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

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。对于每个寻呼机页面和每个选项卡,都会创建一个新 fragment 。我有 3 个选项卡和 3 个页面,因此创建了 9 个 fragment ;

@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 保存当前选项卡和当前 fragment
  2. FragmentA onSaveInstanceState 为寻呼机保存选项卡 fragment
  3. navigationDrawer 第二个选项再次调用 fragment = ControlPanelFragment.newInstance();
  4. ControlPanelFragment onViewCreated 被调用,我可以获得保存的数据信息并创建一个新的分页器和标签
  5. FragmentA onViewCreated 被调用,我可以提取保存的数据
  6. FragmentADetail onActivityCreated 获取保存的数据并正确加载数据(至少我认为)从现在开始,第二组方法被第二次调用,之前保存的数据被重置,所以现在显示错误的数据
  7. ControlPanelFragment onSaveInstanceState 但现在 savedInstanceState 为空
  8. ControlPanelPagerAdapter getItem 调用实例化 3 个 fragment
  9. FragmentA onSaveInstanceState 现在已调用但 savedInstanceState 为空
  10. FragmentADetail onActivityCreated 已调用,但现在选项卡 = 0

谁能解释一下我怎样才能阻止第 7 步到第 10 步的发生?

最佳答案

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

当我在做的时候:

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

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

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 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com