Search code examples
androidandroid-intentandroid-edittextandroid-dialogfragment

Get and Put extra, DialogFragment


I'm writing an app where the user describes a problem and then receives advice. The user presses a button which shows a dialog with an EditText. Once the user presses OK, I want to get their input, but I'm having trouble with the extras. I've read similar questions, but I can't seem to find the problem. On a summary screen where I display the information, no text ever appears. Any help is appreciated!

I think the problem is when I call getText() on the EditText. Using log.d the mText is just an empty String.

Here is my code:

The fragment AdviceFragment from which the dialog is called:

private static final String DIALOG_TEXT = "text";
private static final int REQUEST_TEXT = 0;
private Advice mAdvice;
private boolean hasText;

...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
    ...

    mTextButton = (Button) v.findViewById(R.id.textButton);
    mTextButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            InputTextFragment dialog = new InputTextFragment();
            dialog.setTargetFragment(AdviceFragment.this, REQUEST_TEXT);
            dialog.show(fm, DIALOG_TEXT);
        }
    });
}

...

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode != Activity.RESULT_OK)
    {
        return;
    }

    if (resultCode == REQUEST_TEXT)
    {
        String text = data.getStringExtra(InputTextFragment.EXTRA_TEXT);
        if (text.length() > 0)
        {
            mAdvice.setText(text);
            hasText = true;
        }
        else
        {
            mAdvice.setText(null);
            hasText = false;
        }
    }

InputTextFragment dialog:

public class InputTextFragment extends DialogFragment
{
    public static final String EXTRA_TEXT = "text";
    private String mText;

    private void sendResult(int resultCode)
    {
        if (getTargetFragment() == null)
        {
            return;
        }

        Intent i = new Intent();
        i.putExtra(EXTRA_TEXT, mText.toString());

        getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_input_text, null);

        final EditText editText = new EditText(getActivity());

        return new AlertDialog.Builder(getActivity())
                .setView(v)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        String input = editText.getText().toString();
                        if (input.length() > 0)
                        {
                            mText = input;
                        }
                        else
                        {
                            return;
                        }

                        sendResult(Activity.RESULT_OK);               
                    }
                })
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
                {
                    ...
                })
                .create();
    }
}

Solution

  • final EditText editText = new EditText(getActivity());
    

    You problem is here, your EditText is not added to your Dialog's View Tree. I think you should do like this:

    final EditText editText = (EditText)v.findViewById(your_edittext_id);
    

    Thanks, I think this has fixed part of the problem. Using log.d in the onClick() method of setPositiveButton()shows that it is successfully assigning the value. However, I'm still not getting anything when I call onActivityResult(). Do you have any idea what's going wrong?

    Look here, another typo problem:

       if (resultCode == REQUEST_TEXT)
        {
    

    It should be requestCode.

    I think this should fix your problem, but you'd better follow bean_droid's suggest and use an interface instead of calling the onActivityResult() method. It's because that method may be called by other part of your code, which you don't want.