Reputation: 1291
I have a dialogFragment where there's an edit text. I would like to pass the text to the parent activity, when the positive button of the dialog is clicked, but it doesn't seem to call the method of the interface implemented in the activity. Code: DialogFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setView(R.layout.fragment_newfile);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onFileTyped(textNewFile.getText().toString());
Log.w("Positive","Button"); //This log is showed
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
MainActivity
@Override
public void onFileTyped(String fileName) {
Log.w("New File", ""); //This log is not showed
MainFragment frag = (MainFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
File newFile = new File(frag.getCurrentDir().getAbsolutePath(), "fileName");
}
Listener assignment
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof onFileTypedListener) {
listener = (onFileTypedListener) activity;
} else {
throw new RuntimeException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
Upvotes: 0
Views: 59
Reputation: 12559
I tried your code and it works in my case. Displays both log messages when clicked on positive button. Compare my code with yours and see if there is anything different:
Activity:
public class DialogFragmentActivity extends AppCompatActivity implements MyDialogFragment.onFileTypedListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogfragment);
MyDialogFragment.newInstance("title").show(getSupportFragmentManager(),"MyDialogFragment");
}
@Override
public void onFileTyped(String txt) {
Log.w("yay", "it works");
}
}
Fragment:
public class MyDialogFragment extends DialogFragment {
onFileTypedListener listener;
public interface onFileTypedListener{
public void onFileTyped(String txt);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setView(R.layout.fragment_newfile);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onFileTyped("hello");
Log.w("Positive","Button"); //This log is showed
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof onFileTypedListener) {
listener = (onFileTypedListener) activity;
} else {
throw new RuntimeException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
public static MyDialogFragment newInstance(String title) {
Bundle args = new Bundle();
args.putString("title",title);
MyDialogFragment fragment = new MyDialogFragment();
fragment.setArguments(args);
return fragment;
}
}
Upvotes: 1