I'm learning Dagger2 and trying to keep it simple example and isolate other features as possible as i can to understand required parts.
*Edit: When i started learning, i'm still, Dagger seems too confusing for a beginner, i was asking if you can inject same User to same Activity whenever that Activity is created. User and Activity should have 1 to 1 relationship.
- Activity1 <- User(100) Numbers are arbitrary hashes
- Activity2 <-User(101)
- Activity3 <- User(102)
This is when first time Activities are created.
My question was you navigate back to Activity2 from Activity3, then if it's possible to inject User(101) again only using Dagger? You get back to Activity1 and you get User(100) not new User or Singleton object from component created inside Application.
I was thinking Dagger keeps object like a Pool does and linking Activity instances with objects hashes or keeping states or inner references about injected objects, I was asking if it's possible.*
How can i inject same instance of an object unique to an Activity, it's created when that Activity is created for the first time, and injected again when i navigate to or start that Activity using @ActivityScope? I use the snippets below to construct this example
public class User {
private String name;
private String email;
public User() {
name = "Unknown";
email = "[email protected]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Scope
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}
Module class
@Module
public class UserModule {
@ActivityScope
@Provides
User provideUser() {
return new User();
}
}
Component
@ActivityScope
@Component(modules = {UserModule.class})
public interface UserComponent {
void inject(MainActivity mainActivity);
void inject(SecondActivity secondActivity);
void inject(ThirdActivity thirdActivity);
}
I get component from MyApplication and inside MainActivity to test injected objects using userComponent = DaggerUserComponent.create();
public class MainActivity extends AppCompatActivity {
@Inject
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserComponent daggerUserComponent = DaggerUserComponent.create();
daggerUserComponent.inject(this);
TextView textView = findViewById(R.id.textView);
textView.setText("Main User: " + user.hashCode());
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
This one returns different User every time device is rotated and it's expected behavior since new UserComponent is created every time.
public class SecondActivity extends AppCompatActivity {
@Inject
User user;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
}
});
((MyApplication) getApplication()).getUserComponent().inject(this);
TextView textView = findViewById(R.id.textView);
textView.setText("Second User: " + user.hashCode());
}
}
Application class
public class MyApplication extends Application {
private UserComponent userComponent;
@Override
public void onCreate() {
super.onCreate();
userComponent = DaggerUserComponent.create();
}
public UserComponent getUserComponent() {
return userComponent;
}
}
SecondActivity and ThirdActivity uses UserComponent from MyApplication.
My question is why don't these classes get User unique for each Activity?
They are injected with same User, how can i make these classes to have their own User even after navigation between?
Applicationclass? - Fred Porciúncula@Singletonand return same User for ActivitySecond and ActivityThird - Thracian