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.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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
And the important thing is you need to override onAttach() method
And in the on Button click call the interface method like
And inside the Fragment or activity from which you are launching the dialog should contain setTargetFragment attribute.
And finally you should implement the interface (that was declared inside the dialog fragment) and override the method
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)
This method ensures that the calling fragment implements the onChangeListener of the dialog.
FragmentA (calling fragment):
MyDialogFragment:
What you want, according to Android Developers...
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:
The
Fragment.onActivityResult()
method is useful in this situation. It takesgetTargetRequestCode()
, which is a code you set up between fragments so they can be identified. In addition, it takes a request code, normally just0
if the code worked well, and then anIntent
, which you can attach a string too, like soAlso, 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:The class to send data (the DialogFragment) would use this Fragment we just defined to send the data:
To receive the data, we use this type of class in the Fragment which initially started the DialogFragment:
At this point, you have the string from your
EditText
from theDialogFragment
in the parent fragment. Just use thesendResult(int)
method in yourTextChangeListener()
anonymous class so that the text is sent when you need it.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
In the Fragment where I select a Player, I use the following code in the onCreate method to bind the ViewModel
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)
And finally, in the fragment where you need to show the Player information, do the following in the onCreate method