【问题标题】:Interface value is null when called in MVP by using Dagger2使用 Dagger2 在 MVP 中调用时接口值为 null
【发布时间】:2016-11-08 01:34:10
【问题描述】:

我正在使用 Firebase push notification 结构 MVP 使用 `Dagger2. 因为我喜欢更灵活、逻辑更好的编码。

所以现在,我的问题是notificationView 接口我只能从中获取null 值,即使我已经使用BaseTopActivityBaseTopActivity 中设置了notificationViewDagger 2 (@inject BaseTopPresenter) + @ 987654331@(BaseTopPresenter)

请帮我考虑获取notificationView的值(不应该是null),

谢谢,

p/s : 以下是文件详情。

NotificationView interface

public interface BaseTopView extends BaseView {

   interface NotificationView {
        void onUpdateNotifications(RemoteMessage remoteMessage, int notifyId);
    }
}

Component

@ActivityScope
@Component(dependencies = {AppComponent.class})
public interface Component {

    // Activities
    void inject(BaseTopActivity activity);

    // Services
    void inject(FirebaseMsgService service);

}

BaseActivity.java

@ActivityScope
public abstract class BaseActivity extends AppCompatActivity implements BaseView {

/**
 * Dagger 2
 */
Component Component;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Dagger 2
    Component = DaggerComponent.builder()
    .appComponent(MainApplication.getAppComponent(this)).build();
}

}

BaseTopActivity.java 是我设置接口notificationView 值的地方

@ActivityScope
public abstract class BaseTopActivity extends BaseActivity implements BaseTopView.NotificationView {

@Inject
BaseTopPresenter baseTopPresenter;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * Dagger2
         */
        getComponent().inject(this);

        // notification view : print : Activity Name extend BaseTopActivity

        Timber.e(" setView " + notificationView);

        // BaseTopPresenter value == BaseTopPresenter@2353dad4
        baseTopPresenter.setView(notificationView);

     }
}

BaseTopPresenter interface 是我设置接口notificationView 值的地方

public void setView(BaseTopView.NotificationView notificationView) {
        // GOT VALUE ALREADY HERE, NOT NULL
        Timber.e(BaseTopPresenter.class.getSimpleName() + " setView " + notificationView);

        this.notificationView = notificationView;
    }

    public BaseTopView.NotificationView getView() {
        return notificationView;
    }

问题将出现在我得到接口notificationView值的这个文件中:FirebaseService.java

public class FirebaseMsgService extends FirebaseMessagingService {

// Dagger2
Component Component;

@Inject
BaseTopPresenter presenter;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    MainApplication applicationContext = (MainApplication) getApplicationContext();
    if (applicationContext != null) {
        // RUN
        Timber.e("onMessageReceived --- inject ");

        // Dagger 2
        Component = DaggerComponent.builder()
                .appComponent(MainApplication.getAppComponent(this)).build();
        asukabuComponent.inject(this);
    } else Timber.e("applicationContext == null");

    // VALUE in getView() IS NULL HERE, NO CRASH WITH PRESENTER
    // BaseTopPresenter value == BaseTopPresenter@333cb1bf
    Timber.e("getView " + presenter.getView());

    if (remoteMessage != null)  sendNotification(remoteMessage);
}

更新:BaseTopPresenter 值不同。我认为我注入 Dagger2 的地方是错误的。

MainApplication.java

public class MainApplication extends Application {

/**
 * Dagger 2
 */
private AppComponent appComponent;

/**
 * @param context
 * @return
 */
public static AppComponent getAppComponent(Context context) {
    return ((MainApplication) context.getApplicationContext()).appComponent;
}

@Override
public void onCreate() {
    super.onCreate();

    // Dagger 2
    appComponent = DaggerAppComponent.builder()
            .appModule(new AppModule(this))
            .networkModule(new NetworkModule(getString(R.string.base_url)))
            .build();
}
}

【问题讨论】:

标签: java android mvp dagger-2


【解决方案1】:

正如我所见,您在将其注入 FirebaseMsgService 时创建了 另一个 BaseTopPresenter。显然没有附加视图。您可以制作 BaseTopPresenter 单例(检查 @Singleton 注释)。但在这种情况下,您应该非常小心视图(例如 Activity)生命周期。

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多