Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ba51c3d0 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

New fragment stuff: back stack, and layout integration.

You can now have fragment transactions pushed on to a local back
stack, which will automatically be popped when the user pressed
back in the activity.

Fragments can also now be inserted into layouts.

Change-Id: Id1c9ae3fbc54f696cd8bb5ca5957bec4d3eabf18
parent 108817f3
Loading
Loading
Loading
Loading
+29 −3
Original line number Diff line number Diff line
@@ -20484,6 +20484,17 @@
<parameter name="exitAnim" type="int">
</parameter>
</method>
<method name="popBackStack"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="registerForContextMenu"
 return="void"
 abstract="false"
@@ -24938,7 +24949,7 @@
 visibility="public"
>
</method>
<method name="onLowMemory"
<method name="onInflate"
 return="void"
 abstract="false"
 native="false"
@@ -24948,8 +24959,12 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="activity" type="android.app.Activity">
</parameter>
<parameter name="attrs" type="android.util.AttributeSet">
</parameter>
</method>
<method name="onPause"
<method name="onLowMemory"
 return="void"
 abstract="false"
 native="false"
@@ -24960,7 +24975,7 @@
 visibility="public"
>
</method>
<method name="onRestart"
<method name="onPause"
 return="void"
 abstract="false"
 native="false"
@@ -25081,6 +25096,17 @@
<parameter name="containerViewId" type="int">
</parameter>
</method>
<method name="addToBackStack"
 return="android.app.FragmentTransaction"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="commit"
 return="void"
 abstract="true"
+107 −11
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -39,7 +40,6 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.text.Selection;
import android.text.SpannableStringBuilder;
@@ -52,6 +52,7 @@ import android.util.Log;
import android.util.SparseArray;
import android.view.ContextMenu;
import android.view.ContextThemeWrapper;
import android.view.InflateException;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -69,6 +70,7 @@ import android.view.ViewGroup.LayoutParams;
import android.view.accessibility.AccessibilityEvent;
import android.widget.AdapterView;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;

@@ -652,9 +654,19 @@ public class Activity extends ContextThemeWrapper

    final FragmentManager mFragments = new FragmentManager();
    
    private final class FragmentTransactionImpl implements FragmentTransaction {
    private final Object[] sConstructorArgs = new Object[0];

    private static final Class[] sConstructorSignature = new Class[] { };

    private static final HashMap<String, Constructor> sConstructorMap =
            new HashMap<String, Constructor>();
    
    private final class FragmentTransactionImpl implements FragmentTransaction,
            Runnable, BackStackState {
        ArrayList<Fragment> mAdded;
        ArrayList<Fragment> mRemoved;
        boolean mAddToBackStack;
        boolean mCommitted;
        
        public FragmentTransaction add(Fragment fragment, int containerViewId) {
            return add(fragment, null, containerViewId);
@@ -692,7 +704,18 @@ public class Activity extends ContextThemeWrapper
            return this;
        }

        public FragmentTransaction addToBackStack() {
            mAddToBackStack = true;
            return this;
        }

        public void commit() {
            if (mCommitted) throw new IllegalStateException("commit already called");
            mCommitted = true;
            mHandler.post(this);
        }
        
        public void run() {
            if (mRemoved != null) {
                for (int i=mRemoved.size()-1; i>=0; i--) {
                    mFragments.removeFragment(mRemoved.get(i));
@@ -700,13 +723,28 @@ public class Activity extends ContextThemeWrapper
            }
            if (mAdded != null) {
                for (int i=mAdded.size()-1; i>=0; i--) {
                    mFragments.addFragment(mAdded.get(i));
                    mFragments.addFragment(mAdded.get(i), false);
                }
            }
            mFragments.moveToState(mFragments.mCurState, true);
            if (mAddToBackStack) {
                mFragments.addBackStackState(this);
            }
        }
        
        public void popFromBackStack() {
            if (mAdded != null) {
                for (int i=mAdded.size()-1; i>=0; i--) {
                    mFragments.removeFragment(mAdded.get(i));
                }
            }
            if (mFragments != null) {
                mFragments.moveToState(mFragments.mCurState);
            if (mRemoved != null) {
                for (int i=mRemoved.size()-1; i>=0; i--) {
                    mFragments.addFragment(mRemoved.get(i), false);
                }
            }
            mFragments.moveToState(mFragments.mCurState, true);
        }
    }
    
    private static final class ManagedCursor {
@@ -859,6 +897,7 @@ public class Activity extends ContextThemeWrapper
    protected void onCreate(Bundle savedInstanceState) {
        mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
                com.android.internal.R.styleable.Window_windowNoDisplay, false);
        mFragments.dispatchCreate(savedInstanceState);
        mCalled = true;
    }

@@ -1968,14 +2007,24 @@ public class Activity extends ContextThemeWrapper
        return false;
    }
    
    /**
     * Pop the last fragment transition from the local activity's fragment
     * back stack.  If there is nothing to pop, false is returned.
     */
    public boolean popBackStack() {
        return mFragments.popBackStackState(mHandler);
    }
    
    /**
     * Called when the activity has detected the user's press of the back
     * key.  The default implementation simply finishes the current activity,
     * but you can override this to do whatever you want.
     */
    public void onBackPressed() {
        if (!popBackStack()) {
            finish();
        }
    }
    
    /**
     * Called when a touch screen event was not handled by any of the views
@@ -3776,17 +3825,64 @@ public class Activity extends ContextThemeWrapper
    }

    /**
     * Stub implementation of {@link android.view.LayoutInflater.Factory#onCreateView} used when
     * inflating with the LayoutInflater returned by {@link #getSystemService}.  This
     * implementation simply returns null for all view names.
     * Standard implementation of
     * {@link android.view.LayoutInflater.Factory#onCreateView} used when
     * inflating with the LayoutInflater returned by {@link #getSystemService}.
     * This implementation handles <fragment> tags to embed fragments inside
     * of the activity.
     *
     * @see android.view.LayoutInflater#createView
     * @see android.view.Window#getLayoutInflater
     */
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        if (!"fragment".equals(name)) {
            return null;
        }
        
        TypedArray a = 
            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
        String fname = a.getString(com.android.internal.R.styleable.Fragment_name);
        int id = a.getInt(com.android.internal.R.styleable.Fragment_id, 0);
        String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
        a.recycle();
        
        Constructor constructor = sConstructorMap.get(fname);
        Class clazz = null;

        try {
            if (constructor == null) {
                // Class not found in the cache, see if it's real, and try to add it
                clazz = getClassLoader().loadClass(fname);
                constructor = clazz.getConstructor(sConstructorSignature);
                sConstructorMap.put(fname, constructor);
            }
            Fragment fragment = (Fragment)constructor.newInstance(sConstructorArgs);
            fragment.onInflate(this, attrs);
            mFragments.addFragment(fragment, true);
            if (fragment.mView == null) {
                throw new IllegalStateException("Fragment " + fname
                        + " did not create a view.");
            }
            return fragment.mView;

        } catch (NoSuchMethodException e) {
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Error inflating class " + fname);
            ie.initCause(e);
            throw ie;

        } catch (ClassNotFoundException e) {
            // If loadClass fails, we should propagate the exception.
            throw new RuntimeException(e);
        } catch (Exception e) {
            InflateException ie = new InflateException(attrs.getPositionDescription()
                    + ": Error inflating class "
                    + (clazz == null ? "<unknown>" : clazz.getName()));
            ie.initCause(e);
            throw new RuntimeException(ie);
        }
    }

    // ------------------ Internal API ------------------
    
    final void setParent(Activity parent) {
@@ -3814,6 +3910,7 @@ public class Activity extends ContextThemeWrapper
        
        mWindow = PolicyManager.makeNewWindow(this);
        mWindow.setCallback(this);
        mWindow.getLayoutInflater().setFactory(this);
        if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
            mWindow.setSoftInputMode(info.softInputMode);
        }
@@ -3847,7 +3944,6 @@ public class Activity extends ContextThemeWrapper

    final void performCreate(Bundle icicle) {
        onCreate(icicle);
        mFragments.dispatchCreate(icicle);
    }
    
    final void performStart() {
+6 −4
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@
package android.app;

import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -59,6 +61,10 @@ public class Fragment implements ComponentCallbacks {
        return mActivity;
    }
    
    public void onInflate(Activity activity, AttributeSet attrs) {
        mCalled = true;
    }
    
    public void onAttach(Activity activity) {
        mCalled = true;
    }
@@ -78,10 +84,6 @@ public class Fragment implements ComponentCallbacks {
        mCalled = true;
    }
    
    public void onRestart() {
        mCalled = true;
    }
    
    public void onResume() {
        mCalled = true;
    }
+54 −12
Original line number Diff line number Diff line
@@ -17,15 +17,21 @@
package android.app;

import android.os.Bundle;
import android.os.Handler;
import android.view.ViewGroup;

import java.util.ArrayList;

interface BackStackState {
    public void popFromBackStack();
}

/**
 * Container for fragments associated with an activity.
 */
class FragmentManager {
    final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    ArrayList<Fragment> mFragments;
    ArrayList<BackStackState> mBackStack;
    
    int mCurState = Fragment.INITIALIZING;
    Activity mActivity;
@@ -130,20 +136,32 @@ class FragmentManager {
        f.mState = newState;
    }
    
    void moveToState(int newState) {
    void moveToState(int newState, boolean always) {
        if (mActivity == null && newState != Fragment.INITIALIZING) {
            throw new IllegalStateException("No activity");
        }
        
        if (!always && mCurState == newState) {
            return;
        }
        
        mCurState = newState;
        if (mFragments != null) {
            for (int i=0; i<mFragments.size(); i++) {
                Fragment f = mFragments.get(i);
                moveToState(f, newState);
            }
        }
    }
    
    public void addFragment(Fragment fragment) {
    public void addFragment(Fragment fragment, boolean moveToStateNow) {
        if (mFragments == null) {
            mFragments = new ArrayList<Fragment>();
        }
        mFragments.add(fragment);
        if (moveToStateNow) {
            moveToState(fragment, mCurState);
        }
    }
    
    public void removeFragment(Fragment fragment) {
@@ -151,33 +169,57 @@ class FragmentManager {
        moveToState(fragment, Fragment.INITIALIZING);
    }
    
    public void addBackStackState(BackStackState state) {
        if (mBackStack == null) {
            mBackStack = new ArrayList<BackStackState>();
        }
        mBackStack.add(state);
    }
    
    public boolean popBackStackState(Handler handler) {
        if (mBackStack == null) {
            return false;
        }
        int last = mBackStack.size()-1;
        if (last < 0) {
            return false;
        }
        final BackStackState bss = mBackStack.remove(last);
        handler.post(new Runnable() {
            public void run() {
                bss.popFromBackStack();
            }
        });
        return true;
    }
    
    public void attachActivity(Activity activity) {
        if (mActivity != null) throw new IllegalStateException();
        mActivity = activity;
    }
    
    public void dispatchCreate(Bundle state) {
        moveToState(Fragment.CREATED);
        moveToState(Fragment.CREATED, false);
    }
    
    public void dispatchStart() {
        moveToState(Fragment.STARTED);
        moveToState(Fragment.STARTED, false);
    }
    
    public void dispatchResume() {
        moveToState(Fragment.RESUMED);
        moveToState(Fragment.RESUMED, false);
    }
    
    public void dispatchPause() {
        moveToState(Fragment.STARTED);
        moveToState(Fragment.STARTED, false);
    }
    
    public void dispatchStop() {
        moveToState(Fragment.CREATED);
        moveToState(Fragment.CREATED, false);
    }
    
    public void dispatchDestroy() {
        moveToState(Fragment.INITIALIZING);
        moveToState(Fragment.INITIALIZING, false);
        mActivity = null;
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -7,5 +7,6 @@ public interface FragmentTransaction {
    public FragmentTransaction add(Fragment fragment, int containerViewId);
    public FragmentTransaction add(Fragment fragment, String name, int containerViewId);
    public FragmentTransaction remove(Fragment fragment);
    public FragmentTransaction addToBackStack();
    public void commit();
}
Loading