【问题标题】:Android: Create new instance of bundle and pass to intent in dagger in mvpAndroid:创建bundle的新实例并在mvp的匕首中传递给意图
【发布时间】:2018-11-19 14:22:17
【问题描述】:

我想在我的项目中使用 MVP 和 dagger。 鉴于我有这个方法,并且在这个方法中我会将一些对象传递给演示者:

@Override
public void onLogin(User user, Cookie cookie, UUID sessionId, List<Permission> permissions) {
    super.onLogin(user, cookie, sessionId, permissions);
    presenter.onLogin(user, cookie, sessionId, permissions);
}

这是我的演讲者:

public class Presenter implements ILogin.LoginPresenter{

    private Context context;

    @Inject
    public Presenter(Context context) {
        this.context = context;
    }

    @Override
    public void onLogin(User user, Cookie cookie, UUID sessionId, List<Permission> permissions) {
        Intent intent = new Intent(context,MainActivity.class);///?
        Bundle bundle = new Bundle();///?
        bundle.putString("USER", user.getUserName());
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

我在模块中什么都没有:

@Module
public class LoginModule {
}

我的问题:

  1. 当我使用匕首时,我在演示者中创建新对象(IntentBundle)是真的吗?

  2. 如何在我的场景中使用匕首?这意味着在模块类中创建意图和捆绑的新实例?

【问题讨论】:

    标签: dagger-2 android-mvp


    【解决方案1】:

    MVP 模式的重点是将业务逻辑与其视图分离。在 Presenter 中不包含任何与 Android 框架相关的代码是一种常见的良好做法(这里,您的 Presenter 依赖于 android 框架中的 ContextIntentBundle)。

    在您的情况下,您不应在演示者中创建 IntentBundle,因为它属于您的视图 (MainActivity)。

    您的onLogin 函数可能如下所示:

    @Override
    public void onLogin(User user, Cookie cookie, UUID sessionId, List<Permission> permissions) {
        // whatever is your business logic
        view.showMainActivity(user);
    }
    

    其中view 是您的MainActivity 实现并注入您的演示者的接口。

    【讨论】:

    • 我认为将这些信息存储到首选项中并不是一个好主意。我认为偏好工作是另一回事,它并不安全。我认为最好使用 eventBus 或其他东西!伙计,你的想法是什么? @本杰明
    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多