問題描述
這個問題已經出現了好幾次,我已經閱讀了所有的答案,但我還沒有看到一個真正可靠的方法來處理這個問題.在我的解決方案中,我使用從調用 Activity
到 AlertDialog
的偵聽器,如下所示:
This question has come up several times and I've read all the answers, but I haven't seen a truly robust way to handle this. In my solution, I am using listeners from the calling Activity
to the AlertDialog
like so:
public class MyDialogFragment extends DialogFragment {
public interface MyDialogFragmentListener {
public void onReturnValue(String foo);
}
public void init(boolean someValue)
{
sSomeValue = someValue;
listeners = new ArrayList<MyDialogFragmentListener>();
}
static boolean sSomeValue;
private static ArrayList<MyDialogFragmentListener> listeners;
public void addMyDialogFragmentListener(MyDialogFragmentListener l)
{
listeners.add(l);
}
public void removeMyDialogFragmentListener(MyDialogFragmentListener l)
{
listeners.remove(l);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.title)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
for (MyDialogFragmentListener listener : listeners) {
listener.onReturnValue("some value");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
// Nothing to do but exit
}
});
if (sSomeValue) {
builder.setMessage(R.string.some_value_message);
} else {
builder.setMessage(R.string.not_some_value_message);
}
// Create the AlertDialog object and return it
return builder.create();
}
}
然后在調用Activity
中,我正常實例化對象,通過init
傳入任何參數并設置我的監聽器.
Then in the calling Activity
, I instantiate the object normally, pass in any arguments through init
and set my listener.
問題出在:當您在對話框打開時旋轉設備并更改方向時,Activity
和 MyDialogFragment
對象都會重新創建.為了確保輸入值不會搞砸,我將初始化值設置為 static
.這對我來說感覺很奇怪,但由于一次只會有一個這樣的對話,我可以接受.問題出在返回值上.原始偵聽器將被調用.這很好,因為該對象仍然存在,但如果需要更新 Activity
上的 UI(存在),它不會被更新,因為 newActivity
實例現在正在控制 UI.
Here's the problem: when you rotate the device and change orientation while the dialog is open, both the Activity
and MyDialogFragment
objects get re-created. To ensure that the input values don't get screwed up, I am setting my initialized values as static
. This feels hacky to me, but since there will only be one such dialog at a time, I am ok with it. Where the problem comes in is with the return value. The original listener will get called. That's fine because the object still exists, but if there is a requirement to update the UI on the Activity
(which there is), it won't get updated because the new Activity
instance is now controlling the UI.
我正在考慮的一個解決方案是將對話框類中的 getActivity()
強制轉換為我的 Activity
并強制對話框本身添加偵聽器,而不是調用 <代碼>活動代碼>做.但這感覺就像是黑客的滾雪球.
One solution I am considering is casting getActivity()
in the dialog class to my Activity
and forcing the dialog itself to add a listener, rather than having the calling Activity
do it. But this just feels like a snowballing of hacks.
優雅地處理這個問題的最佳做法是什么?
What is the best practice for handling this gracefully?
推薦答案
你在正確的軌道上,我按照Android 開發者 - 使用 DialogFragments 文章.
You are on the right track, I follow the method recommended by the Android Developers - Using DialogFragments article.
您創建 DialogFragment 并定義 Activity 將實現的接口,就像您在上面所做的那樣:
You create your DialogFragment and define an interface that the Activity will implement, like you have done above with this:
public interface MyDialogFragmentListener {
public void onReturnValue(String foo);
}
然后在DialogFragment中,當你想將結果返回給Activity時,你將Activity投射到界面中:
Then in the DialogFragment when you want to return the result to the Activity you cast the activity to the interface:
@Override
public void onClick(DialogInterface dialog, int id) {
MyDialogFragmentListener activity = (MyDialogFragmentListener) getActivity();
activity.onReturnValue("some value");
}
然后在 Activity
中實現該接口并獲取值:
Then in the Activity
you implement that interface and grab the value:
public class MyActivity implements MyDialogFragmentListener {
...
@Override
public void onReturnValue(String foo) {
Log.i("onReturnValue", "Got value " + foo + " back from Dialog!");
}
}
這篇關于在Android上將值從Dialog傳回Activity的可靠方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!