Android oncreateview called twice

I consider this question asked here more than once, but I cannot figure out how to solve it.

I have an application that the user makes:

1 - open the navigation and select one option (a fragment is created) (here I select the second option);

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 - The selected option (ControlPanelFragment) is loaded: 2.1 - The control panel has tabs and an icon. A new fragment is created for each page of the pager and for each tab. I have 3 tabs and 3 pages, so 9 fragments were created;

 @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 - In setIconPager(); I have this code:

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

where the ControlPanelPagerAdapter has this code:

 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 and FragmentC have almost the same code:

 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 - Finally, FragmentADetail has this code:

 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")); } } 

Now imagine that I am on FragmentA with the third tab selected. If I rotate the screen, I have this sequence of events:

  • ControlPanelFragment onSaveInstanceState saves the current tab and current fragment
  • FragmentA onSaveInstanceState saves tab fragments for a pager
  • The navigator of the second option again receives the call fragment = ControlPanelFragment.newInstance();
  • ControlPanelFragment onViewCreated , and I can get information about the stored data, as well as create a new pager and tabs
  • FragmentA onViewCreated and I can retrieve the saved data
  • FragmentADetail onActivityCreated gets the saved data and loads the data correctly (at least I think) And now the second set of methods is called the second, and the data that was saved earlier is discarded and therefore incorrect data is now displayed.
  • ControlPanelFragment onSaveInstanceState , but now the value of saveInstanceState is null
  • ControlPanelPagerAdapter getItem is called an instance of three fragments
  • FragmentA onSaveInstanceState its now called, but savedInstanceState is null
  • FragmentADetail onActivityCreated , but now tab = 0

Can someone explain to me how I can stop steps 7 through 10?

+4
source share
1 answer

I will find out what my problem was.

When I did:

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

I created a fragment, and when I rotated, the selectItem(int position) screen was called again, so a new instance of the same object was created as steps 7 and the following. The solution was to check if the fragment had already been created and use it instead of creating a new one. I saved the original fragment with the tag, and they searched for this tag. If the tag existed, use this snippet, otherwise create a new one.

 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(); } } 
+10
source

Source: https://habr.com/ru/post/980085/


All Articles