0

How can I call AppUpdateManager.startUpdateFlowForResult() from viewmodel in android. It requires an activity/fragment as a parameter.

Anik Das
  • 31
  • 1
  • 6
  • Can you edit your question to include your ViewModel code and why exactly you're trying to call something that requires an activity/fragment in a ViewModel? – ianhanniballake Jul 05 '20 at 22:22

1 Answers1

0

you shouldn't, you should use a live data object called state(and perhaps use an enum too) to manage the states in your ViewModel then observe that live data in the activity (or fragment) and do proper action according to the state.
assume you need three states, and somewhere in your ViewModel you need to call AppUpdateManager.startUpdateFlowForResult(), your code should look like this:

enum class State {
    StateOne,
    StateTwo,
    StateThree
}

in your ViewModel:

val state = MutableLiveData<State>()

...

fun somewhere() {
    ....
    // instead of calling AppUpdateManager.startUpdateFlowForResult() set the proper state
    // I assume its stateThree
    state.postValue(State.StateThree)
}

and now, in your activiti's onCreate() :

viewModel.state.observe(this) { yourstate ->
            yourstate?.also { state ->
                when (state) {
                    State.stateOne -> { 
                        // do something
                    }
                    State.stateTwo -> {
                        // do something
                    }
                    State.stateThree -> {
                        AppUpdateManager.startUpdateFlowForResult()
                    }

                }

            }
        }

I hope it's clear enough

Mohsen
  • 1,246
  • 9
  • 22