Snippets Detach / Reinstall vs. Show / Hide

I'm having trouble finding the right path to move through fragments without a pager, and I'm having trouble during configuration changes for screen orientation. I use Show / Hide on fragments to make them visible and functional, but I am wondering if Detach / Attach should be used instead. I also have problems adding things to the back stack, and I think this is also related to using show / hide. Is it better to use Attach / detatch or is there a way to override what the back button does to show / hide the last / current fragment.

Behavior: I have a map fragment and a list fragment along with several others. it all starts right and initially works with orientation changes. When I go to view the list, it fills correctly, but when the orientation changes, the list is redrawn without data in it. The map view is also redrawn and displayed behind my pager header indicator. If someone could point me in the right direction to solve this, that would be great. I suspect this is due to the way I show and hide fragments.

Here I create fragments and add them to the fragment manager. I also showed where I show / hide fragments.

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map_frags); mapViewContainer = LayoutInflater.from(this) .inflate(R.layout.map, null); setupFragments(); showFragment(0); } public void setListData(String name) { bName = name; showFragment(1); } private void setupFragments() { final FragmentManager fm = getSupportFragmentManager(); final FragmentTransaction ft = fm.beginTransaction(); mFragment1 = fm.findFragmentByTag("f1"); if (mFragment1 == null) { mFragment1 = new MenuFragment(); ft.add(mFragment1, "f1"); ft.hide(mFragment1); } mMapFragment = (MapFragment) getSupportFragmentManager() .findFragmentByTag(MapFragment.TAG); if (mMapFragment == null) { mMapFragment = MapFragment.newInstance(0); ft.add(R.id.fragment_container, mMapFragment, MapFragment.TAG); } ft.hide(mMapFragment); myListFragment = (ListFrag) getSupportFragmentManager() .findFragmentByTag(ListFrag.TAG); if (myListFragment == null) { myListFragment = new ListFrag(); ft.add(R.id.fragment_container, myListFragment, ListFrag.TAG); } ft.hide(myListFragment); frag = (frag) getSupportFragmentManager().findFragmentByTag( frag.TAG); if (frag == null) { bacFrag = new frag(); ft.add(R.id.fragment_container, frag, frag.TAG); } ft.hide(bacFrag); ft.commit(); } public void showFragment(int fragIn) { final FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); if (mVisible != null) { if (mVisible == mListFragment) { ft.remove(mListFragment); } else { ft.hide(mVisible); } } switch (fragIn) { case 0: ft.show(mMapFragment); ft.commit(); mVisible = mMapFragment; break; case 1: mListFragment = (ListFragmentDisplay) getSupportFragmentManager() .findFragmentByTag(ListFragmentDisplay.TAG); Toast.makeText(this, "startListFrag", Toast.LENGTH_LONG).show(); if (mListFragment == null) { mListFragment = new ListFragmentDisplay(); ft.add(R.id.fragment_container, mListFragment, ListFragmentDisplay.TAG); } ft.show(mListFragment).commit(); mVisible = mListFragment; break; case 2: ft.show(myfragment).commit(); mVisible = myfragment; break; case 3: ft.show(frag).commit(); mVisible = frag; break; } } 
+7
source share
2 answers

It's not your fault. The problem is that when the orientation changes all the activity, even all the added fragments are destroyed. Thus, none of the data is stored in it. Using android:configChanges="orientation|keyboardHidden" not recommended. Rather, set setRetainInstance(true) for each fragment, and it will work well with your current code.

+4
source

If you want better durability (for example, when an action is temporarily destroyed for space problems), also remember to save the state of your fragments using onSaveInstanceState . setRetainInstance will only work on the next configuration change.

0
source

All Articles