How to get data from static fragment class?

802 Views Asked by At

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?

1

There are 1 best solutions below

3
Knossos On BEST ANSWER

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");