I am building an Android App using MVVM and DataBinding. And I have a function inside my ViewModel that starts an Activity. Is it okay to have an onClick call inside a ViewModel?
Like this.
public class MyViewModel { public void onClick(View view, long productId) { Context context = view.getContext(); Intent intent = new Intent(context, ProductDetailActivity.class); intent.putExtra("productId", productId); context.startActivity(intent); } }
And in my XML:
... android:onClick="@{(v) -> viewModel.onClick(v, viewModel.product.id)}">
Or would it be a best practice to move it to the View and call it from EventBus or Rx and have only POJO in my ViewModel?
You don't need data binding (in fact personally I hate the data binding), but MVVM is my current preferred architecture. All you really need for MVVM is streams of data (or a single stream of UI state) that you subscribe to in the view that are coming from a view model.
To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
MVVM separates your view (i.e. Activity s and Fragment s) from your business logic. MVVM is enough for small projects, but when your codebase becomes huge, your ViewModel s start bloating. Separating responsibilities becomes hard. MVVM with Clean Architecture is pretty good in such cases.
The solution is, in the observable code, remove from the live data the observables linked to specific activity. public class LockActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... mViewModel = ViewModelProviders.
Here is an example of a single activity User-Login android application to show the implementation of the MVVM architecture pattern on projects. The application will ask the user to input the Email ID and password. Based on the inputs received the ViewModel notifies the View what to show as a toast message.
As the principle of MVVM points out that only View (activity/fragment) holds reference to the ViewModel and the ViewModel shouldn't hold reference to any View. In your case, to start an activity, I will do like this: MyViewModel.class
This tutorial is for beginners who want to get started with the MVVM architecture. As this is for beginners, I have done some simplifications. Let's get started. What is MVVM architecture? Set up a new project with Kotlin and other dependencies required. Project Structure. Set up the utils package. Set up the data layer.
In the Project window, right-click the app folder and select New > Activity > Empty Activity. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name. Leave all other properties set to their defaults and click Finish. Android Studio automatically does three things: Creates the DisplayMessageActivity file.
The answer to your question is what is your goal?
If you want to use MVVM for separation of concerns so that you can unit test your Viewmodel
then you should try to keep everything that requires a Context
separate from your Viewmodel
. The Viewmodel
contains the core business logic of your app and should have no external dependencies.
However I like where you are going :) If the decision which Activity is opened lies in the View, then it is very very hard to write a JUnit test for it. However you can pass an object into the Viewmodel
which performs the startActivity()
call. Now in your Unit test you can simply mock this object and verify that the correct Activity
is opened
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With