問(wèn)題描述
我正在遷移當(dāng)前使用 Activity.showDialog(DIALOG_ID);
的對(duì)話框,以使用 android參考.
I'm migrating my dialogs, currently using Activity.showDialog(DIALOG_ID);
, to use the DialogFragment
system as discussed in the android reference.
在我的開(kāi)發(fā)過(guò)程中,使用回調(diào)將某些事件傳遞回打開(kāi)對(duì)話框的活動(dòng)/片段時(shí)出現(xiàn)了一個(gè)問(wèn)題:
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:
這是一個(gè)簡(jiǎn)單對(duì)話框的示例代碼:
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();
}}
下面是一些調(diào)用它的活動(dòng)代碼:
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");
}}
代碼幾乎就是您所看到的參考.問(wèn)題是,一旦您進(jìn)行方向更改,當(dāng)顯示對(duì)話框時(shí),它會(huì)按預(yù)期停止工作-->由于活動(dòng)生命周期,活動(dòng)和對(duì)話框都被重建,對(duì)話框現(xiàn)在沒(méi)有正確的引用新的重建活動(dòng).
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.
我在我的活動(dòng) onResume 方法中添加了以下代碼:
I added the following code to my activitys onResume method:
@Override
protected void onResume() {
super.onResume();
DialogTest.udateListener(this);
}
這樣做,我得到了預(yù)期的行為,當(dāng)方向改變發(fā)生時(shí),對(duì)話框?qū)⑹录l(fā)送回新的重建活動(dòng).
Doing this, I get the expected behavior, and the dialog sends events back to the new rebuilt activity when an orientation change occured.
我的問(wèn)題是:處理在方向更改期間由 FragmentActivity 打開(kāi)的 DialogFragment 之間的回調(diào)的最佳實(shí)踐"是什么?
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?
最好的問(wèn)候
推薦答案
是的,這是我自己一直陷入的常見(jiàn)陷阱.首先讓我說(shuō),您在 onResume()
中調(diào)用 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 中的所有內(nèi)容:
Then you can handle everything in the Receiver like this:
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (getActivity() != null){
// Handle result
}
}
查看 ResultReceiver 無(wú)法適應(yīng)屏幕旋轉(zhuǎn)了解更多詳情.所以最后你可能仍然需要用你的 Activity
重新連接 ResultReceiver
.唯一的區(qū)別是您將 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
.
這篇關(guān)于方向更改時(shí)的 DialogFragment 回調(diào)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!