Skip to content Skip to sidebar Skip to footer

How Can I Make Separate Code On Onchanged() While Observing With Livedata In Mvvm? [not Solved]

So, I implemented public class DriverLoginActivity extends BaseActivity implements Observer And, onChanged overided. @Override public void onChanged(@Nullab

Solution 1:

In MVVM, the ViewModel is designed to manage data. The ideal way would be handling your API calls in the ViewModel. Using LiveData for this is not the ideal use of LiveData.

Your ViewModel can have

publicvoidonLoginClick(View view) {
    //Implement Login API call here
}


publicvoidonRegisterClick(View view){
    //Implement Register API call here

}

And in your activity_main.xml

<Button
            android:id="@+id/btnLogin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:text="Login"
            android:onClick="@{(v) -> LoginViewModel.onLoginClick()}"

<Button
            android:id="@+id/btnRegister"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:text="Register"
            android:onClick="@{(v) -> LoginViewModel.onRegisterClick()}"

Check this https://github.com/MindorksOpenSource/android-mvvm-architecture/tree/master/app/src/main/java/com/mindorks/framework/mvvm/ui/login

Post a Comment for "How Can I Make Separate Code On Onchanged() While Observing With Livedata In Mvvm? [not Solved]"