Below is the LoginActivity class
@Inject
User injectedUser;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login2);
MyApp.getComponent(this).inject(this); // injection completed here
}
@Subscribe public void onLoginCompleted(UserResultEvent event)
{
Log.e(" Finding ", injectedUser.getAuthTokenWebService());
User user = event.user;
injectedUser = user; // injectedUser looses values for next activity
// By setting each value as shown in given below two lines,
//I am able to get values on another activity, in my case, DashBoard class
//injectedUser.setAuthTokenWebService(user.getAuthTokenWebService());
//injectedUser.setUserId(user.getUserId());
Intent intent = new Intent(LoginActivity.this, Dashboard.class);
startActivity(intent);
}
Below is my simple Dagger Module class
@Module
public class MyAppModule
{
private final MyApp app;
public MyAppModule(MyApp app) { this.app = app; }
@Provides @Singleton MyApp provideMyApp() { return app; }
@Provides @Singleton
Application provideApplication(MyApp app) { return app; }
// getting User Object
@Provides @Singleton
User provideUser(){ return new User(); }
}
and my last Dashboard activity class is
@Inject
User user;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dashb_main);
MyApp.getComponent(this).inject(this);
// Here is the problem,
Log.e("Dashboard ", String.valueOf(user.getAuthTokenWebService()));
}
Log.e("Dashboard ", String.valueOf(user.getAuthTokenWebService()));
gives me the default value of AuthToken, but it should print different value as it is set in LoginActivity class i.e injectedUser = user;
But this line has no effect as injectedUser variable does not retain values for the next activity, however it is set singleton in the dagger providerUser method.
I do not want to set each value got from web service by typing code as shown below
injectedUser.setAuthTokenWebService(user.getAuthTokenWebService());
injectedUser.setUserId(user.getUserId());
It works this way but is there any other way that injectedUser can have same values of User object fetched from service or i have to set each value as shown above ?