There are a couple of issues here that are only indirectly related to Dagger 2 areas.
First, the fact that you used the term "ViewModel" suggests that you are trying to use MVVM . One of the characteristic features of MVVM is the separation of layers. However, your code has not reached any separation between the model and the presentation model.
Let's look at this model definition from Eric Evans:
A domain model is a system of abstractions that describes individual aspects of the sphere of knowledge, influence, or activity (domain). 2
Here, your area of expertise is the company and its employees. Looking at your EmployeesViewModel
, it contains at least one field, which is probably better isolated in the model layer.
class EmployeesViewModel { List<Employee> employees;
It may just be a bad choice of name, but I think your intention is to create the right view models, so any answer to this question should solve this. Although the choice is related to the view, the class does not really qualify as an abstraction of the view. The actual review model would probably somehow fit the way Employee is displayed on the screen. Say you have a "name" and a "date of birth" TextViews. Then the view model will expose to methods that provide text, visibility, color, etc. For these TextViews.
Secondly, what you are proposing is using (Singleton) Dagger 2 to communicate between activities. You want the company selected in CompaniesActivity
to be transferred to EmployeesActivity
, and the employee selected to EmployeesActivity
to be transferred to EmployeeDetailActivity
. You ask how to achieve this by forcing them to use the same common global object.
Although this may be technically possible with Dagger 2, the correct approach in Android for exchanging data between actions is to use intentions rather than shared objects. The answers to this question are a really good explanation for this point.
Here is the suggested solution: It is not clear what you are doing to get the List<Company>
. Maybe you are getting from dB, maybe you are getting from cached web request. Be that as it may, encapsulate this in a CompaniesRepository
object. Similarly for EmployeesRepository
.
So you will have something like:
public abstract class EmployeesRepository { List<Employee> getAll(); Employee get(int id); int getId(Employee employee); }
Do something similar for the CompaniesRepository
class. These two search classes can be single-point and can be initialized in your module.
@Module class MainModule { @Provides @Singleton public CompaniesRepository(Dependency1 dependency1) {
Your EmployeesActivity now looks something like this:
class EmployeesActivity extends Activity { @Inject CompaniesRepository companiesRepository; @Inject EmployeesRepository employeesRepository; @Override protected void onCreate(Bundle b) {
Extend this example with two other steps, and you will come closer to the standard architecture for the Android application, and you will use Dagger 2 without mixing your model layer, view level, etc.