之前开发遇到过一个问题:
在一个继承自PreferenceActivity的类上绑定了PreferenceScreen界面,并添加了ActionBar实现返回效果.
在其界面里面还嵌套了新的PreferenceScreen,但是通过嵌套PreferenceScreen实现的二级页面却没有ActionBar.
然后就开始看PreferenceScreen的源码并在网上查找相关信息,后来还是手动为二级嵌套的PreferenceScreen添加了ActionBar并设置其点击事件.
系统的action_bar里面的左上角返回按钮竟然没有id,不得不遍历子控件找出类型为imageButton的控件,再添加click事件...
鉴于这是一个通用问题,便把这个逻辑提到了工具类里,方便其他界面使用.代码如下:
/**
* PreferenceScreen use a dialog to display the screen.Here add a clickListener for actionBar's
* imageButton in the upper left corner in this screen. It will trigger the method dialog.onBackPressed()
* to dismiss the dialog when user clicked.
*
* @param context
* @param preferenceScreen
*/
public static void initializeActionBar(Context context, PreferenceScreen preferenceScreen) {
if (preferenceScreen == null) {
return;
}
Dialog dialog = preferenceScreen.getDialog();
ImageButton imageButton = null;
if (dialog == null) {
return;
}
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
Window win = dialog.getWindow();
View mDecorView = win.getDecorView();
ViewGroup sceneRoot = (ViewGroup) mDecorView.findViewById(getInternalId(context, "action_bar"));
if (sceneRoot == null) {
return;
}
for (int i = 0; i < sceneRoot.getChildCount(); i++) {
if (sceneRoot.getChildAt(i) instanceof ImageButton) {
imageButton = (ImageButton) sceneRoot.getChildAt(i);
break;
}
}
if (imageButton == null) {
return;
}
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.onBackPressed();
}
});
}
public static int getInternalId(Context context, String idName) {
Resources resources = context.getResources();
int id = resources.getIdentifier(idName, "id", "android");
return id;
}
然而测试总有各种各样的办法找出问题,操作是这样的:
在这个二级界面点home返回主界面后,去设置里更改下语言,然后再切回二级界面,发现点击左上角的actionbar返回按钮不管用了.
虽然不常见,但确实需要解决.然后还是得看PreferenceScreen源码,找出出现问题的原因.
原来是切换语言后,view进行了重新绘制,但是PreferenceScreen只是保存了UI基本的结构.里面的mDialog是新建的,不再是老的了.那么之前携带的click事件也不存在了,也就没有了点击返回效果.
下面看看相关源码(源码本就不多,以下几乎是全部逻辑了)
public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener,
DialogInterface.OnDismissListener {
private Dialog mDialog;
private ListView mListView;
@Override
protected void onClick() {
if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
return;
}
showDialog(null);
}
private void showDialog(Bundle state) {
Context context = getContext();
if (mListView != null) {
mListView.setAdapter(null);
}
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View childPrefScreen = inflater.inflate(
com.android.internal.R.layout.preference_list_fragment, null);
mListView = (ListView) childPrefScreen.findViewById(android.R.id.list);
bind(mListView);
// Set the title bar if title is available, else no title bar
final CharSequence title = getTitle();
// 这里新建了dialog
Dialog dialog = mDialog = new Dialog(context, context.getThemeResId());
if (TextUtils.isEmpty(title)) {
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
} else {
dialog.setTitle(title);
}
dialog.setContentView(childPrefScreen);
dialog.setOnDismissListener(this);
if (state != null) {
//初始化的时候state是空的,执行onRestoreInstanceState时才会走这里
//state不为空,所以重新绘制后会保留之前添加的UI部分
dialog.onRestoreInstanceState(state);
}
// Add the screen to the list of preferences screens opened as dialogs
getPreferenceManager().addPreferencesScreen(dialog);
dialog.show();
}
public void onDismiss(DialogInterface dialog) {
mDialog = null;
getPreferenceManager().removePreferencesScreen(dialog);
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
final Dialog dialog = mDialog;
if (dialog == null || !dialog.isShowing()) {
return superState;
}
//退出前保存UI逻辑
final SavedState myState = new SavedState(superState);
myState.isDialogShowing = true;
myState.dialogBundle = dialog.onSaveInstanceState();
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
if (myState.isDialogShowing) {
//由于之前有actionbar,所以会将保存的状态传到是哦我Dialog里面
showDialog(myState.dialogBundle);
}
}
private static class SavedState extends BaseSavedState {
boolean isDialogShowing;
Bundle dialogBundle;
public SavedState(Parcel source) {
super(source);
isDialogShowing = source.readInt() == 1;
dialogBundle = source.readBundle();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(isDialogShowing ? 1 : 0);
dest.writeBundle(dialogBundle);
}
public SavedState(Parcelable superState) {
super(superState);
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
从源码看,我们需要在新的mDialog里重新加一次点击事件,正好可以加在调用原生onRestoreInstanceState方法之后.
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
initializeActionBar(...);
}