4

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");
        }
mauricemoss
  • 150
  • 3
  • 9
  • You are creating intent in dialog-fragment, where are you reading it? – Gokhan Arik Aug 12 '13 at 23:48
  • Oh, sry. Didn't make that clear. I already get a NPE when I try to add some data. So creation seems to fail. – mauricemoss Aug 13 '13 at 05:46
  • can you post logcat error – Gokhan Arik Aug 13 '13 at 14:27
  • Man ^^ thx! Sometimes you just do not see what is obvious and you have to be pushed to it again until you see it. The mEditText-TextView is some relict from another implementation and I just didn't get it. – mauricemoss Aug 13 '13 at 19:31
  • Guess I should delete this question, eh? There's no relation at all to the question. – mauricemoss Aug 13 '13 at 19:32
  • You don't need to delete. Just make your question clear. Post your error code, tell us which line breaks. It is easy to ask but not easy to answer. Plus we don't have full code and trying to figure out without running it. – Gokhan Arik Aug 13 '13 at 19:35

1 Answers1

0

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!

mauricemoss
  • 150
  • 3
  • 9