Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from DialogFragment to a Fragment?

Imagine, I have FragmentA from which I startDialogFragment (there are EditText in box). How to can I get back the value from the EditText to FragmentA? I try to make something like this, and this but I was not successful.

like image 543
Kostya Khuta Avatar asked Aug 02 '13 20:08

Kostya Khuta


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.


2 Answers

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.

like image 51
mattpic Avatar answered Oct 06 '22 16:10

mattpic


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)

like image 24
krishnamurthy Avatar answered Oct 06 '22 15:10

krishnamurthy