【发布时间】:2012-10-01 02:18:03
【问题描述】:
我正在迁移我目前使用Activity.showDialog(DIALOG_ID); 的对话框,以使用android reference 中讨论的DialogFragment 系统。
在我的开发过程中使用回调将某些事件传递回打开对话框的活动/片段时出现了一个问题:
这是一个简单对话框的示例代码:
public class DialogTest extends DialogFragment {
public interface DialogTestListener {
public void onDialogPositiveClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
static DialogTestListener mListener;
public static DialogTest newInstance(Activity activity, int titleId, int messageId) {
udateListener(activity);
DialogTest frag = new DialogTest();
Bundle args = new Bundle();
args.putInt("titleId", titleId);
args.putInt("messageId", messageId);
frag.setArguments(args);
return frag;
}
public static void udateListener(Activity activity) {
try {
// Instantiate the NoticeDialogListener so we can send events with it
mListener = (DialogTestListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement DialogTestListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int titleId = getArguments().getInt("titleId");
int messageId = getArguments().getInt("messageId");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// dialog title
builder.setTitle(titleId);
// dialog message
builder.setMessage(messageId);
// dialog negative button
builder.setNegativeButton("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {}});
// dialog positive button
builder.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(DialogTest.this);
}});
// create the Dialog object and return it
return builder.create();
}}
下面是一些调用它的活动代码:
public class SomeActivity extends FragmentActivity implements DialogTestListener {
private EditText mUserName;
@Override
public void onCreate(Bundle savedInstanceState) {
// setup ui
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_user_edit);
// name input
mUserName = (EditText) findViewById(R.id.userEdit_editTextName);
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Log.d(TAG, this.toString());
mUserName.setText(mUserName.getText() + "1");
}
private void showDialog() {
DialogTest test = DialogTest.newInstance(SomeActivity.this, R.string.someTitleText, R.string.someMessageText);
test.show(getSupportFragmentManager(), "testDialog");
}}
代码几乎就是您所看到的参考。问题是,一旦您进行方向更改,当显示一个对话框时,它就会停止按预期工作-->由于活动生命周期,活动和对话框都被重建,对话框现在没有正确的引用新的重建活动。
我在我的活动 onResume 方法中添加了以下代码:
@Override
protected void onResume() {
super.onResume();
DialogTest.udateListener(this);
}
这样做,我得到了预期的行为,当方向改变发生时,对话框将事件发送回新的重建活动。
我的问题是: 处理在方向更改期间由 FragmentActivity 打开的 DialogFragment 之间的回调的“最佳做法”是什么?
最好的问候
【问题讨论】:
-
听起来你已经陷入了许多 Fragment 陷阱。我遇到了和你一样的问题,我在阅读这篇优秀的文章时能够解决它:code.hootsuite.com/orientation-changes-on-android。
标签: android dialog callback orientation-changes android-dialogfragment