DialogFragment callback on orientation change(方向更改时的 DialogFragment 回调)
问题描述
我正在迁移当前使用 Activity.showDialog(DIALOG_ID);
的对话框,以使用 android参考.
I'm migrating my dialogs, currently using Activity.showDialog(DIALOG_ID);
, to use the DialogFragment
system as discussed in the android reference.
在我的开发过程中,使用回调将某些事件传递回打开对话框的活动/片段时出现了一个问题:
There's a question that arose during my development when using callbacks to deliver some event back to the activity/fragment that opened the dialog:
这是一个简单对话框的示例代码:
Here's some example code of a simple dialog:
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();
}}
下面是一些调用它的活动代码:
And here's some activity code calling it:
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");
}}
代码几乎就是您所看到的参考.问题是,一旦您进行方向更改,当显示对话框时,它会按预期停止工作-->由于活动生命周期,活动和对话框都被重建,对话框现在没有正确的引用新的重建活动.
The code is pretty much what you see the reference. Problem is, that once you do a orientation change, when a dialog is shown, it stops working as expected --> Due to the activity lifecycle, both, the activity and the dialog are rebuild, and the dialog now does not have the proper reference to the new rebuilt activity.
我在我的活动 onResume 方法中添加了以下代码:
I added the following code to my activitys onResume method:
@Override
protected void onResume() {
super.onResume();
DialogTest.udateListener(this);
}
这样做,我得到了预期的行为,当方向改变发生时,对话框将事件发送回新的重建活动.
Doing this, I get the expected behavior, and the dialog sends events back to the new rebuilt activity when an orientation change occured.
我的问题是:处理在方向更改期间由 FragmentActivity 打开的 DialogFragment 之间的回调的最佳实践"是什么?
My question is: What is the 'best practice' to handle the callbacks between the DialogFragment which was opened by a FragmentActivity during an orientation change?
最好的问候
推荐答案
是的,这是我自己一直陷入的常见陷阱.首先让我说,您在 onResume()
中调用 DialogTest.udateListener()
的解决方案似乎完全适合我.
Yeah, this is a common trap I'm falling in all the time myself. First of all let me say that your solution of calling DialogTest.udateListener()
in onResume()
seems to be fully appropriate to me.
另一种方法是使用 ResultReceiver
,它可以序列化为 Parcelable
:
An alternative way would be to use a ResultReceiver
which can be serialized as a 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();
}}
然后您可以像这样处理 Receiver 中的所有内容:
Then you can handle everything in the Receiver like this:
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (getActivity() != null){
// Handle result
}
}
查看 ResultReceiver 无法适应屏幕旋转了解更多详情.所以最后你可能仍然需要用你的 Activity
重新连接 ResultReceiver
.唯一的区别是您将 Activity
与 DialogFragment
解耦.
Check out ResultReceiver doesn't survire to screen rotation for more details. So in the end you probably still need to rewire the ResultReceiver
with your Activity
. The only difference is that you decouple the Activity
from the DialogFragment
.
这篇关于方向更改时的 DialogFragment 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:方向更改时的 DialogFragment 回调
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01