Alex Zaitsev
Alex Zaitsev

Reputation: 1781

Fragments state not saved after screen rotation (using ActionBar with tabs)

I use this listener for tabs but when the screen is rotated all fragments move to initial state:

public static class TabListener implements ActionBar.TabListener {

        private final Activity mActivity;
        /**
         * HashMap with tab tag and tab frament
         */
        private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();

        /**
         * Auxiliary class for storing tab info
         * 
         */
        static final class TabInfo {
            private final String mTag;
            private final Class<? extends Fragment> mClass;
            private Fragment mFragment;

            TabInfo(String tag, Class<? extends Fragment> class_) {
                mTag = tag;
                mClass = class_;
            }
        }

        public TabListener(Activity activity) {
            mActivity = activity;
        }

        public void addTab(String tag, Class<? extends Fragment> class_) {
            mTabs.put(tag, new TabInfo(tag, class_));
        }

        /* The following are each of the ActionBar.TabListener callbacks */

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            TabInfo tabInfo = mTabs.get(tab.getTag());

            Fragment prevFragment = ((SherlockFragmentActivity) mActivity)
                    .getSupportFragmentManager().findFragmentByTag(
                            (String) tab.getTag());
            if (prevFragment != null) {
                tabInfo.mFragment = prevFragment;
            }

            if (tabInfo.mFragment == null) {
                // If not, instantiate and add it to the activity
                tabInfo.mFragment = Fragment.instantiate(mActivity,
                        tabInfo.mClass.getName());
                ft.add(android.R.id.content, tabInfo.mFragment, tabInfo.mTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(tabInfo.mFragment);
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            TabInfo tabInfo = mTabs.get(tab.getTag());
            if (tabInfo.mFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(tabInfo.mFragment);
            }
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Usually do nothing.
        }
    }

Upvotes: 1

Views: 2501

Answers (2)

saschoar
saschoar

Reputation: 8230

Call setRetainInstance(true) in your Fragments. That will keep their state even after screen rotations. But be aware of the changes in Fragment lifecycle then, see Docs.

Upvotes: 0

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

...

CustomFragment fragment;
if (savedInstanceState != null) {
    fragment = (CustomFragment) getSupportFragmentManager().findFragmentByTag("customtag");
} else {
    fragment = new CustomFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.container, fragment, "customtag").commit(); 
}

...

}

try this it will surely help you.

Upvotes: 3

Related Questions