我正在使用Dagger2使用Firebase push notification
結構MVP
。因爲我喜歡使用更好的邏輯來編寫更靈活的代碼。使用Dagger2在MVP中調用時,接口值爲null
所以,現在,我的問題是notificationView
接口我只能得到null
值從它甚至我已經在BaseTopActivity
設置notificationView
值利用Dagger 2
(@注入BaseTopPresenter
)+ MVP
(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 == [email protected]
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 == [email protected]
Timber.e("getView " + presenter.getView());
if (remoteMessage != null) sendNotification(remoteMessage);
}
UPDATE: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'參見這裏更多細節https://github.com/saveendhiman/SampleApp/blob/master/app/src/main/java/com/sampleapp/base/SampleApplication.java – Saveen
@Saveen:我能知道你的意思嗎?我也更新了你想要的文件: –
你可以檢查一下我的樣本嗎丟失了什麼 – Saveen