如何将所有值存储到从dagger2注入的类对象?

时间:2015-08-14 18:10:22

标签: java android singleton pass-by-reference dagger-2

以下是LoginActivity类

@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);
}

下面是我简单的Dagger模块类

@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(); }
}

我的上一个Dashboard活动类是

@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()));

给我一​​个AuthToken的默认值,但它应该打印不同的值,因为它在LoginActivity类中设置,即injectedUser = user;
但是这行不起作用,因为injectUser变量不保留下一个活动的值,但是它在dagger providerUser方法中设置为singleton。

我不想通过键入代码来设置从Web服务获得的每个值,如下所示

injectedUser.setAuthTokenWebService(user.getAuthTokenWebService()); 
injectedUser.setUserId(user.getUserId());

它的工作原理是这样的,但是有没有其他方法,injectUser可以从服务中获取相同的User对象值,或者我必须如上所示设置每个值?

1 个答案:

答案 0 :(得分:1)

当然它"失去价值观",you're just altering the pointer to the User object, and not the User object itself.

您需要提供持有人。

public class UserHolder {
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

然后

@Module
public class MyAppModule {
    //...
    @Provides
    @Singleton
    public UserHolder userHolder(User user) {
         UserHolder userHolder = new UserHolder();
         userHolder.setUser(user);
         return userHolder;
    }

    @Provides
    @Singleton
    public User user() {
         return new User();
    }
}

然后

@Inject
UserHolder userHolder;

Log.e("Dashboard ", String.valueOf(userHolder.getUser().getAuthTokenWebService())); 

并且

@Inject
UserHolder userHolder;

@Subscribe public void onLoginCompleted(UserResultEvent event) {

    Log.e(" Finding ", userHolder.getUser().getAuthTokenWebService());

    User user       =   event.user;
    userHolder.setUser(user);