Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过匿名接口进行片段到活动的通信_Java_Android_Android Fragments_Interface - Fatal编程技术网

Java 通过匿名接口进行片段到活动的通信

Java 通过匿名接口进行片段到活动的通信,java,android,android-fragments,interface,Java,Android,Android Fragments,Interface,这可能吗?我一直在使用与onAttach()进行片段活动通信的标准方式,我想知道是否可以这样做: MainActivity.java // Create Dialog passing in callback MyDialogFragment dialog = MyDialogFragment.newInstance(param, new MyDialogInterface) { @Override public void onSelected(DialogFrag

这可能吗?我一直在使用与onAttach()进行片段活动通信的标准方式,我想知道是否可以这样做:

MainActivity.java

// Create Dialog passing in callback
MyDialogFragment dialog = MyDialogFragment.newInstance(param, new MyDialogInterface) {
        @Override
        public void onSelected(DialogFragment dialog, String result) {

        }

        @Override
        public void onNotSelected(DialogFragment dialog) {

        }
    }
// Execute callback
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myInterface.onNotSelected(MyDialogFragment.this);
            }
        })
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myInterface.onSelected(MyDialogFragment.this, "WORKED");
            }
        });
MyDialogFragment.java

// Create Dialog passing in callback
MyDialogFragment dialog = MyDialogFragment.newInstance(param, new MyDialogInterface) {
        @Override
        public void onSelected(DialogFragment dialog, String result) {

        }

        @Override
        public void onNotSelected(DialogFragment dialog) {

        }
    }
// Execute callback
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myInterface.onNotSelected(MyDialogFragment.this);
            }
        })
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myInterface.onSelected(MyDialogFragment.this, "WORKED");
            }
        });

这将允许我在显示对话框的代码中保持逻辑的线性流,而不必向活动添加额外的接口方法。

这可能是一个很好的解决方案,但由于
活动
片段
可以从保存的状态销毁和重新创建,此方案不可靠-在
片段
重新创建后,
片段
内将不会引用回调

应该使用标准方案,将
活动
强制转换到
onAttach()
中的回调接口中,因为
onAttach()
保证在
片段
的重新创建之后调用,并对
活动
进行非空引用。这很不方便,但这是Android框架本身施加的限制

不涉及接口和强制转换的解决方法:

我尝试了许多您希望避免的“界面和铸造”方法的变体,并且所有这些方法都确实感到肮脏和麻烦,因此,现在,我使用这些方法是为了支持
片段
-to-
活动
通信。总体方案如下(仅显示相关部分):

活动:

public class DemoActivity extends Activity {

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onDemoFragmentEvent(DemoFragment.DemoFragmentCallbackEvent event) {
        Toast.makeText(this, "Event in Fragment happened: " + event.getWhatHappened(), Toast.LENGTH_LONG).show();
    }
}
片段:

public class DemoFragment extends Fragment {

    /**
     * Objects of this class will be posted on EventBus if anything happens in this Fragment
     */
    public static class DemoFragmentCallbackEvent {

        private String mWhatHappened;

        public DemoFragmentCallbackEvent(String whatHappened) {
            mWhatHappened = whatHappened;
        }

        public String getWhatHappened() {
            return mWhatHappened;
        }
    }

    public void somethingHappened() {
        EventBus.getDefault().post(new DemoFragmentCallbackEvent("no interfaces and casting"));
    }

}

我有一种感觉就是这样。我想知道是否有人能想出一些聪明的黑客。非常方便@Ron,如果你对“聪明的黑客”持开放态度,那么你可能想尝试我使用的方法,其中包括EventBus。请参见编辑后的答案。