简体   繁体   中英

how to send data like (Date) from activity to bottom sheet dialog fragment?

hi guys I want to send (Date variable) from activity to bottom sheet fragment how can I do this? the image: https://i.stack.imgur.com/8L8E0.png

You can make this by passing the data when you open the bottom sheet dialog.

in the function you open the dialog in it, you can pass whatever data you need. here is an example to what you can do:

in your activity class:

val openDialogToEdit = NewDialogFragment().newInstance(item)
openDialogToEdit.show(supportFragmentManager, TAG)

and in the bottom sheet dialog class you create the new instance function something like this:

fun newInstance(
    item: Item?
): NewDialogFragment {
    val args = Bundle()
    args.putParcelable(KEY_OPEN_DIALOG, item)
    val fragment = NewDialogFragment()
    fragment.arguments = args
    return fragment
}

and in the onViewCreated method in your bottom sheet dialog class, you get the data you send in the bundle something like that:

  item = arguments?.getParcelable(KEY_OPEN_DIALOG)

you can use item instance in whatever you want in your bottom sheet fragment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM