Yt100
Yt100

Reputation: 334

How to efficiently use android-architecture-components?

Recently, I learned about using android architecture components. And I have a question about ViewModel:

According to the architecture design guideline, we can understand that data is the core of the entire application. ViewModel is used to manager data, and the repository is used to fetching data from network or database.

If you have a UI that handles user interaction logic only, But it's almost impossible to get data from a network or a database, So the interactive logic code should be in ViewModel or Activity/Fragment.

In another interface, there is both logical interaction and data acquisition, should I put the interactive logic code into ViewModel or activity/fragment?

Upvotes: 1

Views: 399

Answers (1)

MoGa
MoGa

Reputation: 655

I think you are referring to the popular MVP pattern in Android. In the past it was common to see huge Activity class that handles all the logic that belongs to that Activity.

If you are indeed referring to MVP, then you might like the idea of taking methods that can be separated from the activity to perform the logic in what is called presenter, and leave the activity to worry about rendering. If you want to take that even further, you can also create a class that behaves like a network/database interactor and only use it to fetch data, called from and only from the presenter.

Model is just POJO.

it goes like this:

  • activity --> tells the presenter, hey, a button was pressed, what should I do?
  • presenter --> responds by saying, activity, render that error dialog

You don't have to use libraries to accomplish this, but you will find it to be more effective if combined with dependency injection libraries like the popular framework Dagger.

Upvotes: 1

Related Questions