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
This commit is contained in:
@@ -133,6 +133,7 @@ public class SettingsActivity extends Activity
|
|||||||
// Constants for state save/restore
|
// Constants for state save/restore
|
||||||
private static final String SAVE_KEY_HEADERS_TAG = ":settings:headers";
|
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_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
|
* When starting this activity, the invoking Intent can contain this extra
|
||||||
@@ -313,11 +314,35 @@ public class SettingsActivity extends Activity
|
|||||||
private final ArrayList<Header> mHeaders = new ArrayList<Header>();
|
private final ArrayList<Header> mHeaders = new ArrayList<Header>();
|
||||||
private HeaderAdapter mHeaderAdapter;
|
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) {
|
public TitlePair(Integer first, CharSequence second) {
|
||||||
super(first, 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>();
|
private final ArrayList<TitlePair> mTitleStack = new ArrayList<TitlePair>();
|
||||||
@@ -528,6 +553,18 @@ public class SettingsActivity extends Activity
|
|||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
// We are restarting from a previous saved state; used that to
|
// We are restarting from a previous saved state; used that to
|
||||||
// initialize, instead of starting fresh.
|
// 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 =
|
ArrayList<Header> headers =
|
||||||
savedInstanceState.getParcelableArrayList(SAVE_KEY_HEADERS_TAG);
|
savedInstanceState.getParcelableArrayList(SAVE_KEY_HEADERS_TAG);
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
@@ -643,7 +680,12 @@ public class SettingsActivity extends Activity
|
|||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
last = mTitleStack.size() - 1;
|
last = mTitleStack.size() - 1;
|
||||||
pair = mTitleStack.get(last);
|
pair = mTitleStack.get(last);
|
||||||
if (pair != null) {
|
setTitleFromPair(pair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setTitleFromPair(TitlePair pair) {
|
||||||
final CharSequence title;
|
final CharSequence title;
|
||||||
if (pair.first > 0) {
|
if (pair.first > 0) {
|
||||||
title = getText(pair.first);
|
title = getText(pair.first);
|
||||||
@@ -652,9 +694,6 @@ public class SettingsActivity extends Activity
|
|||||||
}
|
}
|
||||||
setTitle(title);
|
setTitle(title);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Header list
|
* Returns the Header list
|
||||||
@@ -676,6 +715,10 @@ public class SettingsActivity extends Activity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mTitleStack.size() > 0) {
|
||||||
|
outState.putParcelableList(SAVE_KEY_TITLES_TAG, mTitleStack);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user