package com.viven.fragmentstatemanager;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;

public abstract class FragmentStateManager {
    private static final String TAG = "FragmentStateManager";
    private static final boolean DEBUG = false;

    private final FragmentManager mFragmentManager;
    ViewGroup container;

    public FragmentStateManager(ViewGroup container, FragmentManager fm) {
        mFragmentManager = fm;
        this.container = container;
    }

    /**
     * Return the Fragment associated with a specified position.
     */
    public abstract Fragment getItem(int position);

    public Fragment changeFragment(int position) {
        String tag = makeFragmentName(container.getId(), getItemId(position));
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        Fragment fragment = mFragmentManager.findFragmentByTag(tag);
        if (fragment == null) {
            fragment = getItem(position);
            fragmentTransaction.add(container.getId(), fragment, tag);
        } else {
            fragmentTransaction.attach(fragment);
        }

        Fragment curFrag = mFragmentManager.getPrimaryNavigationFragment();
        if (curFrag != null) {
            fragmentTransaction.detach(curFrag);
        }

        fragmentTransaction.setPrimaryNavigationFragment(fragment);
        fragmentTransaction.setReorderingAllowed(true);
        fragmentTransaction.commitNowAllowingStateLoss();

        return fragment;
    }

    public void removeFragment(int position) {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.remove(mFragmentManager
                .findFragmentByTag(makeFragmentName(container.getId(), getItemId(position))));
        fragmentTransaction.commitNowAllowingStateLoss();
    }

    /**
     * Return a unique identifier for the item at the given position.
     * <p>
     * <p>The default implementation returns the given position.
     * Subclasses should override this method if the positions of items can change.</p>
     *
     * @param position Position within this adapter
     * @return Unique identifier for the item at position
     */
    public long getItemId(int position) {
        return position;
    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }
}

@@ -717,24 +717,85 @@
 position);%0A%0A
+    /**%0A     *%0A     * @param position%0A     * @return%0A     */%0A
     public F
@@ -825,32 +825,32 @@
 int position) %7B%0A
-
         String t
@@ -990,32 +990,227 @@
 inTransaction();
+%0A%0A        /*%0A          If fragment manager doesn't have an instance of the fragment, get an instance%0A          and add it to the transaction. Else, attach the instance to transaction.%0A         */
 %0A        Fragmen
@@ -1478,32 +1478,76 @@
 nt);%0A        %7D%0A%0A
+        // Detach existing primary fragment%0A
         Fragment
@@ -1689,32 +1689,130 @@
 ag);%0A        %7D%0A%0A
+        // Set fragment as primary navigator for child manager back stack to be handled by system%0A
         fragment

Add comments