package com.github.mttkay.memento;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

public class Memento {

    public static void retain(FragmentActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();

        final String fragmentTag = activity.getClass().getCanonicalName() + "_state";
        log("Obtaining " + fragmentTag);
        MementoMethods memento = (MementoMethods) fragmentManager.findFragmentByTag(fragmentTag);

        if (memento == null) {
            log("No memento found; storing...");
            memento = createMemento(activity);
            ((MementoCallbacks) activity).onLaunch();
            memento.restore(activity);
            fragmentManager.beginTransaction().add((Fragment) memento, fragmentTag).commit();
        } else {
            log("Found memento; restoring...");
            memento.retain(activity);
        }
    }

    private static MementoMethods createMemento(FragmentActivity activity) {
        final String fragmentClassName = activity.getComponentName().getClassName() + "$Memento";
        try {
            final Class<?> fragmentClass = activity.getClassLoader().loadClass(fragmentClassName);
            return (MementoMethods) fragmentClass.newInstance();
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("Could not load memento: " + fragmentClassName, e);
        } catch (InstantiationException e) {
            throw new IllegalStateException("Could not create memento: " + fragmentClassName, e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Could not access memento: " + fragmentClassName, e);
        }
    }

    private static void log(String message) {
        Log.i(Memento.class.getSimpleName(), message);
    }
}

@@ -29,16 +29,45 @@
 mento;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -98,16 +98,16 @@
 agment;%0A
-
 import a
@@ -422,57 +422,39 @@
 g = 
-activity.getClass().getCanonicalName() + %22_state%22
+getMementoFragmentTag(activity)
 ;%0A  
@@ -1147,63 +1147,37 @@
 e = 
-activity.getComponentName().getClassName() + %22$Memento%22
+getMementoClassName(activity)
 ;%0A  
@@ -1771,32 +1771,32 @@
 tClassName, e);%0A
-
         %7D%0A    %7D%0A
@@ -1788,32 +1788,386 @@
        %7D%0A    %7D%0A%0A
+    private static String getMementoClassName(Activity hostActivity) %7B%0A        return hostActivity.getComponentName().getClassName() + %22$Memento%22;%0A    %7D%0A%0A    private static String getMementoFragmentTag(Activity hostActivity) %7B%0A        // for simplicity, just use the Memento fragment's class name%0A        return getMementoClassName(hostActivity);%0A    %7D%0A%0A
     private stat

Use the Fragment's class name as the fragment tag