How can I make separate code on onChanged() while observing with LiveData in MVVM? [Not solved]

后端 未结 1 381
一生所求
一生所求 2021-01-28 01:30

So, I implemented

public class DriverLoginActivity extends BaseActivity implements Observer

And, onChanged overided.

相关标签:
1条回答
  • 2021-01-28 01:57

    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

    public void onLoginClick(View view) {
        //Implement Login API call here
    }
    
    
    public void onRegisterClick(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

    0 讨论(0)
提交回复
热议问题