I managed to make an ActionBar Tab menu, calling different classes who extend Fragments. The problem is, when I change the orientation, switching between menu items does nothing. But I finally figured out the problem.
The main issue here is old fragment don't being removed when orientation changes, so there is always a copy of an unused tab just above user's selected tab
Any ideas? I am missing something basic?
Thank you
I finally found the solution by myself, in the onTabSelected
method defined in my custom ActionBar.TabListener
class I had ft.add
that added the fragment to my View.
When the orientation is changed the method onTabUnselected
was not called, so the Fragment remained there.
Replacing ft.add
to ft.replace
managed to erase all old fragments so the new ones where correctly displayed.
Hope this helps someone else
I think that's better to save selectedIndex on activity recreation. That way you don't have the problem because the same index is selected and unselected isn't needed and also is nicer for the user.
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
int i = getActionBar().getSelectedNavigationIndex();
outState.putInt("selectedTabIndex", i);
}
//And then restore
private void initActionBar(Bundle savedInstanceState) {
ActionBar ab = getActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ab.addTab(...);
...
if(savedInstanceState != null) {
int index = savedInstanceState.getInt("selectedTabIndex");
getActionBar().setSelectedNavigationItem(index);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With