2016-11-08 55 views
0

我正在使用Dagger2使用Firebase push notification結構MVP。因爲我喜歡使用更好的邏輯來編寫更靈活的代碼。使用Dagger2在MVP中調用時,接口值爲null

所以,現在,我的問題是notificationView接口我只能得到null值從它甚至我已經在BaseTopActivity設置notificationView值利用Dagger 2(@注入BaseTopPresenter)+ MVPBaseTopPresenter

請幫我考慮獲得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(); 
} 
} 
+0

你可以在應用程序類中定義'AppComponent'參見這裏更多細節https://github.com/saveendhiman/SampleApp/blob/master/app/src/main/java/com/sampleapp/base/SampleApplication.java – Saveen

+0

@Saveen:我能知道你的意思嗎?我也更新了你想要的文件: –

+0

你可以檢查一下我的樣本嗎丟失了什麼 – Saveen

回答

0

在我看來,你創建另一 BaseTopPresenter當其注入FirebaseMsgService。顯然沒有附加的觀點。您可以製作BaseTopPresenter單例(檢查@Singleton註釋)。但在這種情況下,您應該非常小心地查看(e.c. Activity)生命週期。

相關問題