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

How can store all values to a class object injected from dagger2?

下面是LoginActivityclass

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

我的最后一个仪表板 activity class 是

@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 class 中设置,即 injectedUser = user;
但是这一行没有效果,因为 injectedUser 变量不保留下一个 activity 的值,但是它在 dagger providerUser 方法中设置为单例。

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

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

它以这种方式工作,但是否有任何其他方式可以使 injectedUser 具有从服务中获取的 User 对象的相同值,或者我必须设置每个值,如上所示?

当然可以"loses values",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);