I got an activity with three fragments and from one of these fragments i'm calling a MyDialogFragment.show()-method. Dialog appears, I do some text-input and try to pass this text-input back to fragment. So far so good.
I set up the back-communication via the onActivityResult() of the 'parent'-fragment in combination with set/getTargetFragment()-calls (see code below). And I do get that result in my parent-fragment, but i cannot pass some data. Every attemp to create an intent and putting some extra-data in it fails. I don't know if I am blind or such.. but shouldn't I be able to create an intent inside a onClick()-method?!
Any help appreciated!
Inside my dialog-fragment:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentFragment().getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_choose_title, null));
builder.setTitle(mTitle)
.setPositiveButton(R.string.dialog_save_title, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getParentFragment().getActivity(), MyMainClass.class);
intent.putExtra("title", mEditText.getText().toString());
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
Inside my fragment:
public void onClick(View v) {
ChooseTitleDialog dialog = ChooseTitleDialog.newInstance(mTimerObject.getmTitle());
dialog.setTargetFragment(mThis, 42);
dialog.show(mFragmentMgr, "choose_title_dialog");
}
And I AM a little blind! While creating intents and thinking about the right context (getActivity(), getParentFragment().getActivity(), ...) or other possibilities to create callbacks to my fragment I DID NOT waste a second thinking about some other reasons for the error. So the EditText-Element I try to get the text from (mEditText) was not initialised :\
Some modification to make it work:
View v = inflater.inflate(R.layout.dialog_choose_title, null);
mEditText = (EditText) v.findViewById(R.id.id_edit_choose_title);
mEditText.setText(mTitle);
builder.setView(v);
Thanks for your time Gokhan!