How to get data from DialogFragment to a Fragment?

The Fragment.onActivityResult() method is useful in this situation. It takes getTargetRequestCode(), which is a code you set up between fragments so they can be identified. In addition, it takes a request code, normally just 0 if the code worked well, and then an Intent, which you can attach a string too, like so

Intent intent = new Intent();
intent.putExtra("STRING_RESULT", str);

Also, the setTargetFragment(Fragment, requestCode) should be used in the fragment that the result is being sent from to identify it. Overall, you would have code in the requesting fragment that looks like this:

FragmentManager fm = getActivity().getSupportFragmentManager();
DialogFragment dialogFragment = new DialogFragment();
dialogFragment.setTargetFragment(this, REQUEST_CODE);
dialogFragment.show();

The class to send data (the DialogFragment) would use this Fragment we just defined to send the data:

private void sendResult(int REQUEST_CODE) {
    Intent intent = new Intent();
    intent.putStringExtra(EDIT_TEXT_BUNDLE_KEY, editTextString);
    getTargetFragment().onActivityResult(
        getTargetRequestCode(), REQUEST_CODE, intent);
}

To receive the data, we use this type of class in the Fragment which initially started the DialogFragment:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Make sure fragment codes match up 
    if (requestCode == DialogFragment.REQUEST_CODE) {
        String editTextString = data.getStringExtra(
            DialogFragment.EDIT_TEXT_BUNDLE_KEY);

At this point, you have the string from your EditText from the DialogFragment in the parent fragment. Just use the sendResult(int) method in your TextChangeListener() anonymous class so that the text is sent when you need it.


Assume a situation that you are uploading some file to server , on clicking of upload button a dialog should open,prompting for title and optional tag.And the dialog itself containing 2 buttons say cancel and continue.

make the UI as you wish by using layout xml file.

then create one class that extending DialogFragment. inflate the layout and initialize views inside onCreateView() method.

Inside that class create one interface

 public interface uploadDialogInterface

   {
       public void senddata(String title, String tag);
   }

    uploadDialogInterface interfaceObj;
    String title="";
    String tag=" ";

And the important thing is you need to override onAttach() method

 @Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context=context;
    interfaceObj= (uploadDialogInterface) getTargetFragment();
}

And in the on Button click call the interface method like

     @Override
public void onClick(View v) {
    int id=v.getId();
    if(id== R.id.vB_fud_cancel)
    {
        dismiss();
    }
    else if(id== R.id.vB_fud_upload)
    {
        title=mVideotitle.getText().toString();
        tag=mOptionaltag.getText().toString();
        if(mVideotitle.getText().toString().isEmpty()) {
            Snackbar.make(mVideotitle,"Please enter the video title", Snackbar.LENGTH_SHORT).show();
       }else
        {
            interfaceObj.senddata(title,tag);
            dismiss();

        }
    }
}

And inside the Fragment or activity from which you are launching the dialog should contain setTargetFragment attribute.

private void callUploadDialog()
{
    UploadDialogFragment fragment = new UploadDialogFragment();
    fragment.setTargetFragment(this, 0);
    FragmentManager manager = getFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_in);
    fragment.show(ft, "UploadDialogFragment");
    fragment.setCancelable(false);
}

And finally you should implement the interface (that was declared inside the dialog fragment) and override the method

@Override
public void senddata(String title,String optionaltag) {
    this.videoTitle=title;
    this.optionalTag=optionaltag;

}

I think this post will be helpful for those who are using dialog fragment for the first time . I was struggled to find the solution . And hopefully this will solve someone's problem in the future. (Sorry for the language)


One of the better and simpler ways to do this is using Android ViewModel.

This helps in easier sharing of data, without the need of sending any data across fragments. You could do this not only for DialogFragments, but also for normal Fragments.

Source: https://developer.android.com/topic/libraries/architecture/viewmodel

Here is what I did

My ViewModel looks as below

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

public class PlayerViewModel extends ViewModel {
    private final MutableLiveData<Player> selectedPlayer = new MutableLiveData<>();

    public LiveData<Player> getSelectedPlayer() {
        return selectedPlayer;
    }

    public void selectPlayer(Player player) {
        selectedPlayer.setValue(player);
    }
}

In the Fragment where I select a Player, I use the following code in the onCreate method to bind the ViewModel

playerViewModel = ViewModelProviders.of(getActivity()).get(PlayerViewModel.class);

When a specific Player is selected, use the following (You can use an ArrayAdapter, DialogFragment's selector or anything you want to display list of players)

playerViewModel = ViewModelProviders.of(getActivity()).get(PlayerViewModel.class);

And finally, in the fragment where you need to show the Player information, do the following in the onCreate method

PlayerViewModel model = ViewModelProviders.of(getActivity()).get(PlayerViewModel.class);
model.getSelectedPlayer().observe(this, new Observer<Player>() {
    @Override
    public void onChanged(@Nullable Player selPlayer) {
        if (selPlayer != null)
            player = selPlayer;
            populateData();
        }
    });

You need to send the data from the dialog back to the activity via a callback method, then have the activity give that data back to the fragment you want it to go to. Just a quick example:

public void datFromDialog(String data){
    MyFragment mf = (MyFragment)getFragmentManager().findFragmentById(r.id.frag);

    mf.iWantNewData(data);
}