2017-03-09 107 views
2

該proides GSON,改造和OkHttpClient匕首2注射不工作

的單身
@Module 
public class MyModule { 

    @Provides 
    @Singleton 
    Gson provideGson() { 
     GsonBuilder gsonBuilder = new GsonBuilder(); 
     return gsonBuilder.create(); 
    } 

    @Provides 
    @Singleton 
    OkHttpClient provideOkHttpClient() { 
     OkHttpClient client = new OkHttpClient(); 
     return client; 
    } 

    @Provides 
    @Singleton 
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { 
     Retrofit retrofit = new Retrofit.Builder() 
       .addConverterFactory(GsonConverterFactory.create(gson)) 
       .baseUrl(BuildConfig.SERVER_BASE_URL) 
       .client(okHttpClient) 
       .build(); 
     return retrofit; 
    } 
} 

,允許注入單身成活性和片段

@Singleton 
@Component(modules={MyModule.class}) 
public interface MyComponent { 

    void inject(Activity activity); 
    void inject(Fragment fragment); 
    void inject(Application application); 
} 

該構建的主應用程序類的部件的模塊組件

public class MyApp extends Application{ 


    private MyComponent component; 

    @Inject 
    Retrofit retrofit; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     component= DaggerMyComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this); // inject retrofit here 
    } 

    public MyComponent getComponent() { 
     return component; 
    } 
} 

這是我所在的片段試圖注入Retrofit。

public class MyFragment extends Fragment { 

    @Inject 
    Retrofit retrofit; 

    @Override 
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
          @Nullable Bundle savedInstanceState) { 
     ((MyApp)getActivity().getApplication()).getComponent().inject(this); 
     .... 

    } 
} 

在MyApp和MyFragment中,retrofit實例都是null。

+0

看起來好像沒什麼問題。 – Raghunandan

+0

我以爲是相同的,但它沒有注入改造:( – Coder

回答

0

你可以注入活動,片段和同Component.You應用需要爲每個活動,片段和組件篩選創建單獨的組件:

Activiy

使用在您的所有活動這個組件:

@Singleton 
@Component(modules={MyModule.class}) 
public interface MyActivityComponent { 
    void inject(Activity activity); 
    void inject(AnotherActivity activity); 
} 

進樣的活動是這樣的:

component= DaggerMyActivityComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this) 

片段

使用該組件在所有的片段:

@Singleton 
    @Component(modules={MyModule.class}) 
    public interface MyFragmentComponent { 

     void inject(Fragment fragment); 
     void inject(AnotherFragmen fragment); 
    } 

進樣的片段是這樣的:

component= DaggerMyFragmentComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this) 

應用

使用這個組件您應用:

@Singleton 
    @Component(modules={MyModule.class}) 
    public interface MyAppComponent { 
     void inject(Application application); 
    } 

進樣在應用這樣的:

component= DaggerMyAppComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this) 
+0

這是有效的。那麼有什麼辦法可以在不創建片段特定的注入方法的情況下在所有片段中注入依賴關係嗎? – Coder

+0

我也遇到了這個問題,但截至目前我不知道沒有任何解決方案 –

+0

而不是'inject(活動活動)'嘗試做注入(MyActivity活動)' –