1

I am working on an application where there is a log in form.I am bit confused with the pattern as I don't understand as how I will open the new activity as my login is successful.As per my understanding when I click on submit button a method in viewmodel which authenticates will get call and after my successful login I do not know how to navigate it to activity file so that I can call new activity.

2
  • Check for example repositories on github and blogs on Medium.com to get started with MVVM. Commented Dec 5, 2018 at 9:36
  • I tried to find but I did not got any satisfactory answer Commented Dec 5, 2018 at 9:57

3 Answers 3

5

Thumb Rule:

No package from android.* should lie in ViewModel. You can ignore package import for ViewModel

Also, you can do it with normal ViewModel as well.

How to proceed?

Lets make it simple. Suppose, you are making Login page.

Lets have below things in ViewModel itself:

  • Fields of email & password with two-way binding
  • Form Validation logic
  • Button Click Event
  • Api Call

All these things lie in your ViewModel.

Now, your Activity needs to react to the result of your Api Call. So, let your ViewModel have a separate LiveData where T is the type of Response from your Api Call.

For example:

val loginApiStatus = MutableLiveData<LoginResponse>()

And then, let your Activity observe this LiveData. It should be observed in onStart() method of Activity. I will tell you the reason why to observe in onStart().

viewModel.loginApiStatus.observe(this, Observer{ loginResponse->
    // respond accordingly
})

Now, once you receive response from Api, simply update the LiveData in your ViewModel as:

loginApiStatus.value = loginResponse // Login Api Response

With this structure, you have total control over handling the Api Response. Even if, your activity goes into background, after launching Api Call, you will still have the state of what happened to that Api call. Now, when you return to Login screen again from background, you start observing the LiveData again (because we are observing the state LiveData in onStart() as I said earlier), and you will get the state to react upon.

Life becomes a lot easier when you start storing states of your View / Fragment / Activity, in your ViewModel itself.

Sign up to request clarification or add additional context in comments.

Comments

0

for that, you can use AndroidViewModel which gives Application Context and then using intent you can navigate to the new activity.

6 Comments

but in mvvm it is said that we should not use the context
I have edited answer, you can use Application context provided by AndroidViewModel
Is it possible to get it done in activity file instead of view model
what about observers and live data? what its use.
Or can we use callback using interface to get the control on activity
|
0

You can simply implement a click listener in activity and handle opening new activity from there. As far as I know, ViewModel should only be used to persist data or do other communication with Repository/model. It should not be used for navigation.

Although if you want to use then you can use AndroidViewModel class which provides a context object that can be used for navigating to another activity/fragment.

3 Comments

Can we use listeners? If we use then how this pattern is different from MVP.As per my understanding it should not have reference of view.
You should consider using LiveData instead.
You can create something like val status =MutableLiveData<Boolean>() set it to true when all verification is done. In Activity, observe on it. and when you get true, you can init navigation from within activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.