【发布时间】:2016-11-08 01:34:10
【问题描述】:
我正在使用 Firebase push notification 结构 MVP 使用 `Dagger2.
因为我喜欢更灵活、逻辑更好的编码。
所以现在,我的问题是notificationView 接口我只能从中获取null 值,即使我已经使用BaseTopActivity 在BaseTopActivity 中设置了notificationView 值Dagger 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();
}
}
【问题讨论】:
-
你能在应用程序类中定义
AppComponent吗?看这里更多细节github.com/saveendhiman/SampleApp/blob/master/app/src/main/java/… -
@Saveen:我能知道你是什么意思吗?我还更新了你想要的文件:)
-
你能回顾一下我的样本你错过了什么
-
使用此示例存储库,您可以了解有关 MVP 与 dagger 混合的更多信息:github.com/mmirhoseini/marvel