我有一个简单的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();
}
}
答案 0 :(得分:1)
onSomeButtonClicked()
callbacks.onSomeButtonClicked(this);
中调用它会没问题
private void onSomeButtonClicked() {
Callbacks callbacks = (Callbacks) getActivity();
callbacks.onSomeButtonClicked(this);
dismiss();
}