Reputation: 341
I have a trouble with saving state of current fragment after changing orientation. I've got fragments with gridview that i replace in navigation process on fragments. Actions after starting app.
1) Starting "MainActivity".
2) Adding "CenterPanelFragment" (this is a container for inner fragment).
3) Replacing in "CenterPanelFragment" fragment "FragmentCatalog"(GridView). Picture 1.
4) If i click on gridview item of "FragmentCatalog", it'll replece for fragment "FragmentCatalogStone"(Gridview). Picture 2.
5) After that i change orientation on a device,as you can see i've got the fragment "FragmentCatalog" in Landscape orientation instead of fragment "FragmentCatalogStone" in Landscape orientation. Picture 3
What things i do wrong? I attached necessary class files.
Sorry for my poor english.
Thanks a lot!
Upvotes: 0
Views: 1651
Reputation: 341
I found a way to resolve the problem.
I simply added android:configChanges="orientation|screenSize"
in manifest for my Activity, and it works!
Upvotes: 1
Reputation: 7108
I might not fully have understood your code but I suspect this bit in your MainActivity's onCreate:
frAdapter = new FragmentAdapter(getSupportFragmentManager());
vPager = (ViewPager)findViewById(R.id.pager);
vPager.setAdapter(frAdapter);
vPager.setCurrentItem(1);
vPager.setOnPageChangeListener(this);
vPager.setOffscreenPageLimit(3);
Think you should save currentItem in onSaveInstance and retrive it:
vPager.setCurrentItem(value_before_orientation_change);
Not 100% sure if the vPager actually overwrites your fragment as I suspect though.
Edit:
Even more likely, in your CenterPanelFragment.java in onActivityCreated you commit the catalog:
fragmentTransaction.replace(R.id.fragment_container, fragmentCatalog);
fragmentTransaction.addToBackStack("FragmentCatalog");
fragmentTransaction.commit();
Maybe something as simple as setRetainInstance(true) in onCreate at CenterPanelFragment could resolve this problem.
I am pretty confident that the orientation change relaunches your CenterPanelFragment, causing a new call to onActivityCreated.
Think you could try in your FragmentCatalog:
And change this line:
fragmentTransaction.replace(R.id.fragment_container, fcAllStone);
To:
fragmentTransaction.replace(R.id.fragment_container, fcAllStone, "fragment_stone");
Now in CentralPanelFragment:
if (savedInstanceState != null)
{
Log.d(MainActivity.tag, "CenterPanelFragment not null");
fragmentCatalog = (FragmentCatalog)getFragmentManager().getFragment(savedInstanceState, FragmentCatalog.class.getName());
// see if stone is open
if (savedInstanceState != null) {
FragmentCatalogStone fragmentStone = (FragmentCatalogStone) getFragmentManager()
.findFragmentByTag("fragment_stone");
if (FragmentCatalogStone != null) {
/*
* maybe recommit it, dont actually think anything is needed
* since moving the below code inside the else statement
* prevents it from overwriting the FragmentCatalogStone
*/
}
}
else
{
Log.d(MainActivity.tag, "CenterPanelFragment null");
fragmentCatalog = new FragmentCatalog();
android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.fragment_container, fragmentCatalog);
fragmentTransaction.addToBackStack("FragmentCatalog");
fragmentTransaction.commit();
}
Upvotes: 0
Reputation: 341
if (savedInstanceState != null)
{
fragmentCatalog = (FragmentCatalog)getFragmentManager().getFragment(savedInstanceState, FragmentCatalog.class.getName());
}
else
{
fragmentCatalog = new FragmentCatalog();
}
Upvotes: 0