Bug 925068: Bookmarks folders are lost on rotation. [r=lucasr]
authorSriram Ramasubramanian <sriram@mozilla.com>
Fri, 18 Oct 2013 17:30:10 -0700
changeset 165280 06cc867fd5d17b2871de4da824b2e8501f55e752
parent 165279 92c512181d1c24eb877198b43eea642cf9fd4132
child 165281 b3778525478565f794546e4b23a1d13ed4d72a0e
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr
bugs925068
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 925068: Bookmarks folders are lost on rotation. [r=lucasr]
mobile/android/base/home/BookmarksListAdapter.java
mobile/android/base/home/BookmarksPage.java
--- a/mobile/android/base/home/BookmarksListAdapter.java
+++ b/mobile/android/base/home/BookmarksListAdapter.java
@@ -9,16 +9,18 @@ import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.util.Pair;
 import android.view.View;
 
+import java.util.Collections;
+import java.util.List;
 import java.util.LinkedList;
 
 /**
  * Adapter to back the BookmarksListView with a list of bookmarks.
  */
 class BookmarksListAdapter extends MultiTypeCursorAdapter {
     private static final int VIEW_TYPE_ITEM = 0;
     private static final int VIEW_TYPE_FOLDER = 1;
@@ -35,25 +37,33 @@ class BookmarksListAdapter extends Multi
 
     // mParentStack holds folder id/title pairs that allow us to navigate
     // back up the folder heirarchy.
     private LinkedList<Pair<Integer, String>> mParentStack;
 
     // Refresh folder listener.
     private OnRefreshFolderListener mListener;
 
-    public BookmarksListAdapter(Context context, Cursor cursor) {
+    public BookmarksListAdapter(Context context, Cursor cursor, List<Pair<Integer, String>> parentStack) {
         // Initializing with a null cursor.
         super(context, cursor, VIEW_TYPES, LAYOUT_TYPES);
 
-        mParentStack = new LinkedList<Pair<Integer, String>>();
+        if (parentStack == null) {
+            mParentStack = new LinkedList<Pair<Integer, String>>();
 
-        // Add the root folder to the stack
-        Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
-        mParentStack.addFirst(rootFolder);
+            // Add the root folder to the stack
+            Pair<Integer, String> rootFolder = new Pair<Integer, String>(Bookmarks.FIXED_ROOT_ID, "");
+            mParentStack.addFirst(rootFolder);
+        } else {
+            mParentStack = new LinkedList<Pair<Integer, String>>(parentStack);
+        }
+    }
+
+    public List<Pair<Integer, String>> getParentStack() {
+        return Collections.unmodifiableList(mParentStack);
     }
 
     // Refresh the current folder by executing a new task.
     private void refreshCurrentFolder() {
         if (mListener != null) {
             mListener.onRefreshFolder(mParentStack.peek().first);
         }
     }
--- a/mobile/android/base/home/BookmarksPage.java
+++ b/mobile/android/base/home/BookmarksPage.java
@@ -13,23 +13,26 @@ import org.mozilla.gecko.home.HomePager.
 
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.Loader;
+import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewStub;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
 
+import java.util.List;
+
 /**
  * A page in about:home that displays a ListView of bookmarks.
  */
 public class BookmarksPage extends HomeFragment {
     public static final String LOGTAG = "GeckoBookmarksPage";
 
     // Cursor loader ID for list of bookmarks.
     private static final int LOADER_ID_BOOKMARKS_LIST = 0;
@@ -38,16 +41,19 @@ public class BookmarksPage extends HomeF
     private static final String BOOKMARKS_FOLDER_KEY = "folder_id";
 
     // List of bookmarks.
     private BookmarksListView mList;
 
     // Adapter for list of bookmarks.
     private BookmarksListAdapter mListAdapter;
 
+    // Adapter's parent stack.
+    private List<Pair<Integer, String>> mSavedParentStack;
+
     // Reference to the View to display when there are no results.
     private View mEmptyView;
 
     // Callback for cursor loaders.
     private CursorLoaderCallbacks mLoaderCallbacks;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -78,17 +84,17 @@ public class BookmarksPage extends HomeF
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
         final Activity activity = getActivity();
 
         // Setup the list adapter.
-        mListAdapter = new BookmarksListAdapter(activity, null);
+        mListAdapter = new BookmarksListAdapter(activity, null, mSavedParentStack);
         mListAdapter.setOnRefreshFolderListener(new OnRefreshFolderListener() {
             @Override
             public void onRefreshFolder(int folderId) {
                 // Restart the loader with folder as the argument.
                 Bundle bundle = new Bundle();
                 bundle.putInt(BOOKMARKS_FOLDER_KEY, folderId);
                 getLoaderManager().restartLoader(LOADER_ID_BOOKMARKS_LIST, bundle, mLoaderCallbacks);
             }
@@ -120,16 +126,20 @@ public class BookmarksPage extends HomeF
         // We use commitAllowingStateLoss() instead of commit() here to avoid
         // an IllegalStateException. If the phone is rotated while Fennec
         // is in the background, onConfigurationChanged() is fired.
         // onConfigurationChanged() is called before onResume(), so
         // using commit() would throw an IllegalStateException since it can't
         // be used between the Activity's onSaveInstanceState() and
         // onResume().
         if (isVisible()) {
+            // The parent stack is saved just so that the folder state can be
+            // restored on rotation.
+            mSavedParentStack = mListAdapter.getParentStack();
+
             getFragmentManager().beginTransaction()
                                 .detach(this)
                                 .attach(this)
                                 .commitAllowingStateLoss();
         }
     }
 
     @Override