Skip to content Skip to sidebar Skip to footer

Android Action Bar Tab With Scrollview Made Duplicate View After Orientation Change

I have a very simple code where I use Action Bar with tab fragments. It works fine after load, but after orientation change it goes crazy. The old fragment also visible (why?). Sor

Solution 1:

I found a usable answer in other question.

I need to modify my TabListener (I moved it into my Main activity class as inner class):

privateclassTabListener<T extendsFragment> implementsandroid.app.ActionBar.TabListener
    {
        private Fragment mFragment;
        privatefinal Activity mActivity;
        privatefinal String mTag;
        privatefinal Class<T> mClass;

        /**
         * Constructor used each time a new tab is created.
         * 
         * @param activity
         *            The host Activity, used to instantiate the fragment
         * @param tag
         *            The identifier tag for the fragment
         * @param clz
         *            The fragment's Class, used to instantiate the fragment
         */publicTabListener(final Activity activity, final String tag, final Class<T> clz)
        {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        @OverridepublicvoidonTabReselected(final Tab tab, final FragmentTransaction ft)
        {
            // User selected the already selected tab. Usually do nothing.
        }

        @OverridepublicvoidonTabSelected(final Tab tab, final FragmentTransaction ft)
        {
            mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if (mFragment == null)
            {
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            }
            else
            {
                ft.attach(mFragment);
            }
        }

        @OverridepublicvoidonTabUnselected(final Tab tab, final FragmentTransaction ft)
        {
            if (mFragment != null)
            {
                ft.detach(mFragment);
            }
        }
    }

So before I add fragment(again), I check it existance (and get its reference) and if it exists I attach it only.

Solution 2:

Try using ft.replace(R.id.content, mFragment) in place of ft.attach(mFragment); in onTabSelected function

Solution 3:

I found a very simple solution to avoid fragments duplication:

publicvoidonTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        Fragment currentFragment = getFragmentManager().findFragmentByTag(CURRENT_FRAGMENT_TAG);
        if (currentFragment == null || !currentFragment.getClass().equals(mFragment.getClass())) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.add(android.R.id.content, mFragment, CURRENT_FRAGMENT_TAG);
        }
    }

    publicvoidonTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
        ft.remove(mFragment);
    }

The key of the solution is in the condition:

currentFragment == null || !currentFragment.getClass().equals(mFragment.getClass())

This condition is valid ONLY if the classes of the fragments are different. In case you have different instances of the same class you have to put an extra attribute in your fragments to recognize his function (or the association with and to make the condition !currentFragment.getClass().equals(mFragment.getClass()) true: for example you can use the FragmentTransaction tag feature.

Bye, Alex.

Solution 4:

publicvoidonTabSelected(Tab tab, FragmentTransaction ft)
    {
        // Check if the fragment is already initializedif (mFragment == null)
        {
            if(ft.findFragmentById(android.R.id.content) == null){
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
            }
        } else
        {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }

    }

Something like that, because your problem is that your adding the same fragment twice, we just have to find where...

Solution 5:

I solved this by just looking up the fragment in the tab listener constructor.

publicclassTabListener<T extendsFragment> implementsActionBar.TabListener 
{
    private Fragment fragment;
    privatefinal SherlockFragmentActivity activity;
    privatefinal String tag;
    privatefinal Class<T> clazz;

    publicTabListener(SherlockFragmentActivity activity, String tag, Class<T> clazz) 
    {
        this.activity = activity;
        this.tag = tag;
        this.clazz = clazz;

        FragmentManagermanager= ((SherlockFragmentActivity) activity).getSupportFragmentManager();
        fragment = manager.findFragmentByTag(tag);
    }
...
}

Post a Comment for "Android Action Bar Tab With Scrollview Made Duplicate View After Orientation Change"