I am confused about the use of fragment in BottomSheet. I used this tutorial: https://blog.mindorks.com/android-bottomsheet-in-kotlin
This thing itself basically works - BottomSheet shows up and hides whenever I want, but I want to pass some data there dynamically , according to the item that I click on from the list.
This is how it works in code according to the tutorial:
// Fragment creation
var bottomSheetFragment : Fragment? = null
bottomSheetFragment = supportFragmentManager.findFragmentById(R.id.filter_fragment)
// Behavior configuration
private var mBottomSheetBehavior: BottomSheetBehavior<View?>? = null
bottomSheetFragment?.let {
BottomSheetBehavior.from(it.view)?.let { bsb ->
bsb.state = BottomSheetBehavior.STATE_HIDDEN
mBottomSheetBehavior = bsb
}
}
}
// How we show and hide it
fun show(){
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
fun hide(){
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
So, there it works and there's no fragment object where I can use NewInstance to pass the data as I usually do. How can I do it in this case?
Thanks!
you can use bundle to pass data from activity to fragment like this
Bundle bundle = new Bundle();
String myMessage = "Stack Overflow is cool!";
bundle.putString("message", myMessage );
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
in the fragment you can access this bundle
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
String myValue = this.getArguments().getString("message");
...
}
You can add BottomSheetFragment
in your nav_graph
and pass data as an argument of action
.
findNavController().navigate(
MyFragmentDirections.actionMyFragmentToBottomSheetDialog(myData)
)
And finally, get the result as mentioned in the document .
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.