Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a BottomSheetFragment need ViewModel?

When working with Bottom Sheets and Dialog how to perform operation:

  1. Use a SharedViewModel with fragment that created this bottom sheet?
  2. Don't use a ViewModel at all?
  3. Creating a separate ViewModel for the BottomSheet?
  4. Any other approach that is best practice
like image 765
Arrowsome Avatar asked Apr 29 '20 06:04

Arrowsome


People also ask

Can a fragment have a ViewModel?

The Fragment library provides two options for communication: a shared ViewModel and the Fragment Result API. The recommended option depends on the use case. To share persistent data with any custom APIs, you should use a ViewModel .

Can a ViewModel be shared between activity and fragment?

In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel. This is one way to have communication between fragments or activities.

How to implement bottomsheetdialogfragment?

If you want to show a different type of bottom sheets in the same activity or want to make reusable bottom sheet the should use BottomSheetDialogFragment. The process for this is very similar to that of making a DialogFragment. As in any DialogFragment, Firstly you have to create a layout file for implement Bottom Sheet fragment

How to create a ViewModel from a fragment?

Use activity's scope to create an instance of the ViewModel in fragment so that all the fragments and activity now has a common ViewModel and have access to the same ViewModelStore. If the activity is re-created, it and all its fragments receive the same ViewModel instance that was created by the associated activity.

How to show different types of bottom sheets in the same activity?

If you want to show a different type of bottom sheets in the same activity or want to make reusable bottom sheet the should use BottomSheetDialogFragment. The process for this is very similar to that of making a DialogFragment.

How to make a modal bottom sheet in Salesforce?

For making a modal type of bottom sheet, you will have to extend BottomSheetDialogFragment and set the custom view. Thirdly just need to make an instance of your MyBottomSheetFragment and call a method show ().


2 Answers

  1. If the bottom sheet/dialog is tightly bound to your "host" fragment (it shares some specific live data), and it is never going to be created from some other fragment, then it's OK to use a shared view model.
  2. If the dialog is dead simple (like one input + 2 buttons), then the viewmodel might not be needed
  3. If the dialog really needs a viewmodel (i.e. it fetches and displays some dynamic data), then a separate viewmodel makes sense
like image 197
Alex Timonin Avatar answered Oct 17 '22 11:10

Alex Timonin


I'll go with the first approach by using a ShareViewModel, but if you understand the underlying layer, shared ViewModel is also ViewModel it's just a name convention we gave it to them.

Also sometimes it becomes tedious to write separate ViewModel to deal with fragments and bottom sheet where a MainActivity ViewModel can also do the exact same thing.

What I meant, in order to avoid complexity I use one view model per activity. Now, whenever I want to execute something in fragment or bottom sheet I just pass the view model in the constructor itself. Many people will think this is bad practice but it's not coz as per the concept of view model it will only be created and destroyed in accordance with the activity's lifecycle and only one instance will be created all along. Also by doing this, I can use Dependency injection with the fragment (I don't think that DI works with navigation component but I think you got my point).

like image 22
kaustubhpatange Avatar answered Oct 17 '22 11:10

kaustubhpatange