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

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

Fix some problems with moving in and out of detached state.

Loaders were not being re-initialized correctly when coming back
(this would also impact the back stack).  The ListView also wasn't
working correctly, and there were also problems with simply
re-using a Fragment instance after it had been removed.

Change-Id: I534b091ae09c0ef7ffffe9d68049e6840e8926b3
parent 2bb2d48f
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -30614,6 +30614,17 @@
 visibility="public"
>
</method>
<method name="isDetached"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="isHidden"
 return="boolean"
 abstract="false"
+3 −1
Original line number Diff line number Diff line
@@ -4413,7 +4413,9 @@ public class Activity extends ContextThemeWrapper
        mFragments.dispatchStart();
        if (mAllLoaderManagers != null) {
            for (int i=mAllLoaderManagers.size()-1; i>=0; i--) {
                mAllLoaderManagers.valueAt(i).finishRetain();
                LoaderManagerImpl lm = mAllLoaderManagers.valueAt(i);
                lm.finishRetain();
                lm.doReportStart();
            }
        }
    }
+52 −5
Original line number Diff line number Diff line
@@ -587,11 +587,6 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
        mWho = "android:fragment:" + mIndex;
   }
    
    final void clearIndex() {
        mIndex = -1;
        mWho = null;
    }
    
    final boolean isInBackStack() {
        return mBackStackNesting > 0;
    }
@@ -782,6 +777,15 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
        return mActivity != null && mAdded;
    }

    /**
     * Return true if the fragment has been explicitly detached from the UI.
     * That is, {@link FragmentTransaction#detach(Fragment)
     * FragmentTransaction.detach(Fragment)} has been used on it.
     */
    final public boolean isDetached() {
        return mDetached;
    }

    /**
     * Return true if this fragment is currently being removed from its
     * activity.  This is  <em>not</em> whether its activity is finishing, but
@@ -1202,6 +1206,35 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
        }
    }

    /**
     * Called by the fragment manager once this fragment has been removed,
     * so that we don't have any left-over state if the application decides
     * to re-use the instance.  This only clears state that the framework
     * internally manages, not things the application sets.
     */
    void initState() {
        mIndex = -1;
        mWho = null;
        mAdded = false;
        mRemoving = false;
        mResumed = false;
        mFromLayout = false;
        mInLayout = false;
        mRestored = false;
        mBackStackNesting = 0;
        mFragmentManager = null;
        mActivity = mImmediateActivity = null;
        mFragmentId = 0;
        mContainerId = 0;
        mTag = null;
        mHidden = false;
        mDetached = false;
        mRetaining = false;
        mLoaderManager = null;
        mLoadersStarted = false;
        mCheckedForLoaderManager = false;
    }

    /**
     * Called when the fragment is no longer attached to its activity.  This
     * is called after {@link #onDestroy()}.
@@ -1429,6 +1462,13 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
        }
    }

    void performStart() {
        onStart();
        if (mLoaderManager != null) {
            mLoaderManager.doReportStart();
        }
    }

    void performStop() {
        onStop();
        
@@ -1447,4 +1487,11 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
            }
        }
    }

    void performDestroyView() {
        onDestroyView();
        if (mLoaderManager != null) {
            mLoaderManager.doReportNextStart();
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -803,7 +803,7 @@ final class FragmentManagerImpl extends FragmentManager {
                    if (newState > Fragment.STOPPED) {
                        if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
                        f.mCalled = false;
                        f.onStart();
                        f.performStart();
                        if (!f.mCalled) {
                            throw new SuperNotCalledException("Fragment " + f
                                    + " did not call through to super.onStart()");
@@ -856,7 +856,7 @@ final class FragmentManagerImpl extends FragmentManager {
                            }
                        }
                        f.mCalled = false;
                        f.onDestroyView();
                        f.performDestroyView();
                        if (!f.mCalled) {
                            throw new SuperNotCalledException("Fragment " + f
                                    + " did not call through to super.onDestroyView()");
@@ -1006,7 +1006,7 @@ final class FragmentManagerImpl extends FragmentManager {
        }
        mAvailIndices.add(f.mIndex);
        mActivity.invalidateFragmentIndex(f.mIndex);
        f.clearIndex();
        f.initState();
    }
    
    public void addFragment(Fragment fragment, boolean moveToStateNow) {
+20 −4
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ public class ListFragment extends Fragment {
    TextView mStandardEmptyView;
    View mProgressContainer;
    View mListContainer;
    boolean mSetEmptyText;
    CharSequence mEmptyText;
    boolean mListShown;

    public ListFragment() {
@@ -210,6 +210,9 @@ public class ListFragment extends Fragment {
    public void onDestroyView() {
        mHandler.removeCallbacks(mRequestFocus);
        mList = null;
        mListShown = false;
        mEmptyView = mProgressContainer = mListContainer = null;
        mStandardEmptyView = null;
        super.onDestroyView();
    }

@@ -289,10 +292,10 @@ public class ListFragment extends Fragment {
            throw new IllegalStateException("Can't be used with a custom content view");
        }
        mStandardEmptyView.setText(text);
        if (!mSetEmptyText) {
        if (mEmptyText == null) {
            mList.setEmptyView(mStandardEmptyView);
            mSetEmptyText = true;
        }
        mEmptyText = text;
    }
    
    /**
@@ -346,6 +349,9 @@ public class ListFragment extends Fragment {
                        getActivity(), android.R.anim.fade_out));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_in));
            } else {
                mProgressContainer.clearAnimation();
                mListContainer.clearAnimation();
            }
            mProgressContainer.setVisibility(View.GONE);
            mListContainer.setVisibility(View.VISIBLE);
@@ -355,6 +361,9 @@ public class ListFragment extends Fragment {
                        getActivity(), android.R.anim.fade_in));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_out));
            } else {
                mProgressContainer.clearAnimation();
                mListContainer.clearAnimation();
            }
            mProgressContainer.setVisibility(View.VISIBLE);
            mListContainer.setVisibility(View.GONE);
@@ -383,6 +392,8 @@ public class ListFragment extends Fragment {
                    com.android.internal.R.id.internalEmpty);
            if (mStandardEmptyView == null) {
                mEmptyView = root.findViewById(android.R.id.empty);
            } else {
                mStandardEmptyView.setVisibility(View.GONE);
            }
            mProgressContainer = root.findViewById(com.android.internal.R.id.progressContainer);
            mListContainer = root.findViewById(com.android.internal.R.id.listContainer);
@@ -400,12 +411,17 @@ public class ListFragment extends Fragment {
            }
            if (mEmptyView != null) {
                mList.setEmptyView(mEmptyView);
            } else if (mEmptyText != null) {
                mStandardEmptyView.setText(mEmptyText);
                mList.setEmptyView(mStandardEmptyView);
            }
        }
        mListShown = true;
        mList.setOnItemClickListener(mOnClickListener);
        if (mAdapter != null) {
            setListAdapter(mAdapter);
            ListAdapter adapter = mAdapter;
            mAdapter = null;
            setListAdapter(adapter);
        } else {
            // We are starting without an adapter, so assume we won't
            // have our data right away and start with the progress indicator.
Loading