可靠的方法,从对话框回传值到活动在Android? [英] Robust way to pass value back from Dialog to Activity on Android?

查看:434
本文介绍了可靠的方法,从对话框回传值到活动在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题又拿出了好几次,我读过所有的答案,但我还没有看到一个真正可靠的方法来处理这​​个问题。在我的解决方案,我用的听众从调用活动 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();
    }
}

然后在调用活动,我实例化对象通常,传递任何参数通过的init 并设置我侦听器。

Then in the calling Activity, I instantiate the object normally, pass in any arguments through init and set my listener.

下面的问题:当你旋转设备,并改变方向,而对话是开放的,无论是活动 MyDialogFragment 对象将会重新创建。为了确保输入的数值没有得到搞砸了,我设置我的初始化值静态。这种感觉哈克给我,但因为只会有一次一个这样的对话,我确定它。当问题出现在与返回值。原来的侦听器将被调用。这很好,因为对象仍然存在,但如果有需要更新的活动的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()在弹出的对话框类到我的活动并迫使对话本身添加一个监听器,而不是调用活动做到这一点。但是,这只是感觉像黑客滚雪球。

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并定义的活动将实现,就像你在上面这个做一个接口:

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:

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");
}

然后在活动您实现该接口,并抓住值:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆