0

I am trying to Inject activity's view model and I want use it inside Dialog Fragment, how to inject it with Kodein? and use the view model which I've inject before in other activity or fragments.

I've try tutorial from medium here is the tutorial

https://proandroiddev.com/android-viewmodel-dependency-injection-with-kodein-249f80f083c9

I am trying to access injection result from activity's view model but when I try access some object inside the viewModel from Dialog Fragment the value is null not the same with activity's viewModel

// This is from Activity
private val viewModelFactory: TriplogisticViewModelFactory by instance()
    private val viewModel: TriplogisticViewModel by lazy {
        ViewModelProviders
            .of(this@ContactDetailBottomSheetDialogFragment, viewModelFactory)
            .get(TriplogisticViewModel::class.java)
}
Log.e("VIEWMODEL_ACTIVITY", viewModel.mode.get().toString) // result is SENDER_MODE

I am expecting when I access some object inside the dialog fragment's viewModel, I got same value as activity's viewModel

// This is from Dialog Fragment
private val viewModelFactory: TriplogisticViewModelFactory by instance()
    private val viewModel: TriplogisticViewModel by lazy {
        ViewModelProviders
            .of(this@ContactDetailBottomSheetDialogFragment, viewModelFactory)
            .get(TriplogisticViewModel::class.java)
}

I want same result as activity's viewModel object but I got null result

Log.e("VIEWMODEL_FRAGMENT", viewModel.mode.get().toString) // result is null
Hyper Space
  • 11
  • 1
  • 3
  • 1. Which kodein-di module are you using ? 2. How do you bind your factory ? If you followed the previous article you may have use the provider mechanism from kodein-di, meaning the binding method is call everytime you inject it. Thus, this could have influence on injection, depending on how you did the binding. – romainbsl Aug 30 '19 at 20:03

1 Answers1

0

You have to use shared ViewModel. Use activity owner for instantiating the ViewModel. Looks like:

class SharedViewModel : ViewModel() {
    val selected = MutableLiveData<Item>()

    fun select(item: Item) {
        selected.value = item
    }
}

class MasterFragment : Fragment() {

    private lateinit var itemSelector: Selector

    private lateinit var model: SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        model = activity?.run {
            ViewModelProviders.of(this)[SharedViewModel::class.java]
        } ?: throw Exception("Invalid Activity")
        itemSelector.setOnClickListener { item ->
            // Update the UI
        }
    }
}

class DetailFragment : Fragment() {

    private lateinit var model: SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        model = activity?.run {
            ViewModelProviders.of(this)[SharedViewModel::class.java]
        } ?: throw Exception("Invalid Activity")
        model.selected.observe(this, Observer<Item> { item ->
            // Update the UI
        })
    }
}

For details click here

Abu Noman
  • 435
  • 3
  • 13