1

I have an edittext in a fragment in a static class which extends DialogFragment. I need get user input from edittext inside the fragment. I tried instantiating container activity and passing the value but it doesn't work because I need to make it final, I think. Please see the code below of the static class:

public class DetailDaily extends BaseActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
        ...................
        ..................
        String name;
        ........
public static class DialogCreater extends DialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final DetailDaily dd = new DetailDaily();
            int title = getArguments().getInt("Title");
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Set the dialog title
            switch (title) {
                case 1:
                    ....................................
                case 2:
                    final LayoutInflater inflater = getActivity().getLayoutInflater();
                    final View mView = inflater.inflate(R.layout.edit_dialog, null);
                    builder.setView(mView)
                            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int id) {
                                    EditText userInput = (EditText) mView.findViewById(R.id.dialogEdit);
                                    dd.name = userInput.toString(); 
                                }
                            })
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                    break;
            }


            return builder.create();
        }
    }

How to read the userInput out of static class?

Murat K.
  • 35
  • 6

1 Answers1

1

Firstly, unless you really need the DialogFragment to be declared in your Activity, move it into a separate class (not necessarily static). Additionally, why on earth are you creating an Activity instance in your FragmentDialog?

Secondly, the standard way of calling a DialogFragment is to use a newInstance() function. You can pass in a callback interface to deliver results to your Activity.

Please review the Android documentation for an example.

public static interface Callback {
    public void onResult(String result);
}

static MyDialogFragment newInstance(MyDialogFragment.Callback callback) {
    MyDialogFragment f = new MyDialogFragment();

    f.setCallback(callback);

    return f;
}

You can call mCallback.onResult(editTextResult); when you have something you want to return to the Activity.

Then call your DialogFragment with:

// FragmentTransaction boilerfluff here
DialogFragment newFragment = MyDialogFragment.newInstance(new MyDialogFragment.Callback() {
    @Override
    public void onResult(String result) {
        // Do stuff
    }
});
newFragment.show(ft, "dialog");
Knossos
  • 15,802
  • 10
  • 54
  • 91
  • Thanks for the answer. I am trying to make an alarm clock. So I'm trying to use the fragments to get settings like name of the alarm, clock, days of week, etc... So all the values I get from fragments are in different types. Is there a way that I can accomplish getting all those in one callback or what is the correct way to do it. Thanks in advance. – Murat K. Aug 15 '15 at 13:35
  • 1
    Of course, just change the callback however you require. `public void onResult(String result);` could turn into `public void onResult(String clockName, int daysOfWeek, ...);` – Knossos Aug 15 '15 at 16:56
  • I think my question was not clear. I mean I make a different fragment for each setting I want to get in one dialogfragment class as above with case 1, case2... One fragment for 'name', one for 'time' etc.. The answer still works for that? Also can you please edit your answer to show which method goes to main activity and which goes to fragment class? Sorry for the much trouble :) – Murat K. Aug 15 '15 at 17:24