Fix title when changing from portrait to landscape configuration

- save the titles stack during onSaveInstanceState(...)
- set it back when creating the activity if there is a savedInstanceState
and restore the title to the last item in the stack

Change-Id: Ic6c2714f5474275c9f55cc4d6c70d14f6a8cd993
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 74fe86e..213bc18 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -133,6 +133,7 @@
     // Constants for state save/restore
     private static final String SAVE_KEY_HEADERS_TAG = ":settings:headers";
     private static final String SAVE_KEY_CURRENT_HEADER_TAG = ":settings:cur_header";
+    private static final String SAVE_KEY_TITLES_TAG = ":settings:titles";
 
     /**
      * When starting this activity, the invoking Intent can contain this extra
@@ -313,11 +314,35 @@
     private final ArrayList<Header> mHeaders = new ArrayList<Header>();
     private HeaderAdapter mHeaderAdapter;
 
-    private class TitlePair extends Pair<Integer, CharSequence> {
+    static private class TitlePair extends Pair<Integer, CharSequence> implements Parcelable {
 
         public TitlePair(Integer first, CharSequence second) {
             super(first, second);
         }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(first);
+            TextUtils.writeToParcel(second, dest, flags);
+        }
+
+        TitlePair(Parcel in) {
+            super(in.readInt(), TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in));
+        }
+
+        public static final Creator<TitlePair> CREATOR = new Creator<TitlePair>() {
+            public TitlePair createFromParcel(Parcel source) {
+                return new TitlePair(source);
+            }
+            public TitlePair[] newArray(int size) {
+                return new TitlePair[size];
+            }
+        };
     }
 
     private final ArrayList<TitlePair> mTitleStack = new ArrayList<TitlePair>();
@@ -528,6 +553,18 @@
         if (savedInstanceState != null) {
             // We are restarting from a previous saved state; used that to
             // initialize, instead of starting fresh.
+
+            ArrayList<TitlePair> titles =
+                    savedInstanceState.getParcelableArrayList(SAVE_KEY_TITLES_TAG);
+            if (titles != null) {
+                mTitleStack.addAll(titles);
+            }
+            final int lastTitle = mTitleStack.size() - 1;
+            if (lastTitle >= 0) {
+                final TitlePair last = mTitleStack.get(lastTitle);
+                setTitleFromPair(last);
+            }
+
             ArrayList<Header> headers =
                     savedInstanceState.getParcelableArrayList(SAVE_KEY_HEADERS_TAG);
             if (headers != null) {
@@ -643,19 +680,21 @@
             if (size > 0) {
                 last = mTitleStack.size() - 1;
                 pair = mTitleStack.get(last);
-                if (pair != null) {
-                    final CharSequence title;
-                    if (pair.first > 0) {
-                        title = getText(pair.first);
-                    } else {
-                        title = pair.second;
-                    }
-                    setTitle(title);
-                }
+                setTitleFromPair(pair);
             }
         }
     }
 
+    private void setTitleFromPair(TitlePair pair) {
+        final CharSequence title;
+        if (pair.first > 0) {
+            title = getText(pair.first);
+        } else {
+            title = pair.second;
+        }
+        setTitle(title);
+    }
+
     /**
      * Returns the Header list
      */
@@ -676,6 +715,10 @@
                 }
             }
         }
+
+        if (mTitleStack.size() > 0) {
+            outState.putParcelableList(SAVE_KEY_TITLES_TAG, mTitleStack);
+        }
     }
 
     @Override