方向更改时的DialogFragment回调

dar*_*aga 18 android dialog callback orientation-changes android-dialogfragment

我正在迁移我正在使用的对话框,Activity.showDialog(DIALOG_ID);以便DialogFragmentandroid参考中讨论使用该 系统.

在使用回调将一些事件传递回打开对话框的活动/片段时,我的开发过程中出现了一个问题:

这是一个简单对话框的示例代码:

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();
}}
Run Code Online (Sandbox Code Playgroud)

以下是调用它的一些活动代码:

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");
}}
Run Code Online (Sandbox Code Playgroud)

代码几乎就是你看到的引用.问题是,一旦你进行了方向更改,当显示一个对话框时,它会按预期停止工作 - >由于活动生命周期,两者都重建了活动和对话框,现在对话框没有正确的参考新的重建活动.

我将以下代码添加到我的活动onResume方法:

    @Override
protected void onResume() {
    super.onResume();
    DialogTest.udateListener(this);
}
Run Code Online (Sandbox Code Playgroud)

这样做,我得到了预期的行为,当对话发生更改时,对话框会将事件发送回新的重建活动.

我的问题是:处理在方向更改期间由FragmentActivity打开的DialogFragment之间的回调是什么"最佳实践"?

最好的祝福

Rom*_*ych 14

有更好的解决方案,而不是使用静态方法和变量,因为它只适用于对话框的一个实例.最好将回调存储为非静态成员

private DialogTestListener mListener;
public void setListener (DialogTestListener listener){
  mListener = listener;
}
Run Code Online (Sandbox Code Playgroud)

然后你应该像这样使用TAG显示你的对话框 mDialogFragment.show(getSupportFragmentManager(), DIALOG_TAG);

然后在onResume您的活动方法中,您可以重置您的监听器

protected void onResume() {
   super.onResume();
   mDialogFragment = (CMFilterDialogFrg) getSupportFragmentManager().findFragmentByTag(DIALOG_TAG);
   if(mDialogFragment  != null){
       mDialogFragment.setListener(yourListener)
   }
}
Run Code Online (Sandbox Code Playgroud)


And*_*dré 8

是的,这是一个常见的陷阱我自己一直在堕落.首先让我说,你打电话的解决方案DialogTest.udateListener()onResume()似乎完全适合我.

另一种方法是使用一个ResultReceiver可以序列化为Parcelable:

public class DialogTest extends DialogFragment {

public static DialogTest newInstance(ResultReceiver receiver, int titleId, int messageId) {
    DialogTest frag = new DialogTest();
    Bundle args = new Bundle();
    args.putParcelable("receiver", receiver);
    args.putInt("titleId", titleId);
    args.putInt("messageId", messageId);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int titleId = getArguments().getInt("titleId");
    int messageId = getArguments().getInt("messageId");
    ResultReceiver receiver = getArguments().getParcelable("receiver");

    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) {
            receiver.sendResult(Activity.RESULT_CANCEL, null);
        }});
    // dialog positive button
    builder.setPositiveButton("Yes", new OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            receiver.sendResult(Activity.RESULT_OK, null);
        }});

    // create the Dialog object and return it
    return builder.create();
}}
Run Code Online (Sandbox Code Playgroud)

然后您可以像这样处理Receiver中的所有内容:

protected void onReceiveResult(int resultCode, Bundle resultData) {
    if (getActivity() != null){
        // Handle result
    }
}
Run Code Online (Sandbox Code Playgroud)

检查结果ResultReceiver无法生存到屏幕旋转以获取更多详细信息.所以最后你可能还需要重新ResultReceiver连接你的Activity.唯一的区别是你Activity从中解耦了DialogFragment.

  • 查看Fragment #setRetainInstance(boolean)并阅读文档.这实际上是我们真正想要的解决方案.虽然活动仍在被销毁和重新创建,但片段仍被保留并重用.因此,回调DialogTestListener仍然指向正确的对象,您不必在配置更改后重新连接片段. (2认同)