我应该在DialogFragment本身(在回调方法之前)还是在Activity(在回调方法中)中解除DialogFragment?

时间:2014-10-22 08:12:45

标签: android android-dialogfragment

我有一个简单的DialogFragment包含一些数据和一个按钮

public class SimpleDialogFragment extends DialogFragment {

    public static interface Callbacks {
        void onButtonClicked(SimpleDialogFragment dialogFragment);
    }

    static SimpleDialogFragment newInstance() {
        return new SimpleDialogFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_dialog_simple, null);
        contentView.findViewById(R.id.some_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSomeButtonClicked();
            }
        })
        return contentView;
    }

    private void onSomeButtonClicked() {
        Callbacks callbacks = (Callbacks) getActivity();
        // 
        // Should I dismiss here?
        // dismiss();
        // 
        callbacks.onSomeButtonClicked(this);
    }

}



public class SimpleActivity extends Activity implements SimpleDialogFragment.Callbacks {

    @Override
    public void onSomeButtonClicked(SimpleDialogFragment dialogFragment) {
        // 
        // Or should I dismiss here?
        // dialogFragment.dismiss();
        doSomething();
    }
}

1 个答案:

答案 0 :(得分:1)

onSomeButtonClicked()

之后,在callbacks.onSomeButtonClicked(this);中调用它会没问题
private void onSomeButtonClicked() { 
    Callbacks callbacks = (Callbacks) getActivity();

    callbacks.onSomeButtonClicked(this);
    dismiss();
}