2017-06-09 60 views
1

我正在嘗試將MainActivity注入片段。我有一個在我的MainActivity中實現的接口,它將偵聽來自Fragment的事件。所以我想注入MainActivity並在其上調用事件監聽器。如何使用Dagger2將活動注入片段

我曾嘗試做以下事情,但未能如願。只顯示代碼片段。

接口

public interface RecipeItemListener { 
    void onRecipeItem(); 
} 

MainActivity實現接口

public class MainActivity extends AppCompatActivity implements RecipeItemListener { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(savedInstanceState == null) { 
      getSupportFragmentManager() 
        .beginTransaction() 
        .add(R.id.main_fragment_container, RecipeListView.newInstance(), RecipeListView.TAG) 
        .commit(); 
     } 
    } 

    @Override 
    public void onRecipeItem() { 
     Timber.d("onRecipeItem"); 
    } 
} 

我的模塊,它提供在MainActivity

@Module 
public class RecipeListModule { 
    private MainActivity mainActivity; 

    public RecipeListModule(MainActivity mainActivity) { 
     this.mainActivity = mainActivity; 
    } 

    @RecipeListScope 
    @Provides 
    public RecipeItemListener providesRecipeListMainActivity() { 
     return mainActivity; 
    } 
} 

我主要成分

@Singleton 
@Component(modules = { 
     AndroidModule.class, 
     NetworkModule.class}) 
public interface BusbyBakingComponent { 
    RecipeListComponent add(RecipeListModule recipeListModule); 
} 

我的子

@RecipeListScope 
@Subcomponent(modules = {RecipeListModule.class}) 
public interface RecipeListComponent { 
    void inject(RecipeListView target); 
} 

我的應用程序類

public class BusbyBakingApplication extends Application { 
    private BusbyBakingComponent applicationComponent; 
    private RecipeListComponent recipeListComponent; 

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

     applicationComponent = createApplicationComponent(); 
    } 

    public BusbyBakingComponent createApplicationComponent() { 
     return DaggerBusbyBakingComponent.builder() 
       .networkModule(new NetworkModule()) 
       .androidModule(new AndroidModule(BusbyBakingApplication.this)) 
       .build(); 
    } 

    public BusbyBakingComponent getApplicationComponent() { 
     return applicationComponent; 
    } 

    public RecipeListComponent createRecipeListComponent(MainActivity activity) { 
     recipeListComponent = applicationComponent.add(new RecipeListModule(activity)); 
     return recipeListComponent; 
    } 

    public void releaseRecipeListComponent() { 
     recipeListComponent = null; 
    } 
} 

,在我的片段,這是如何,我試圖注入:

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

    ((BusbyBakingApplication)getActivity().getApplication()) 
      .createRecipeListComponent((MainActivity)getActivity()) 
      .inject(RecipeListView.this); 
} 

我不斷收到以下錯誤:

Error:(14, 8) error: [me.androidbox.busbybaking.di.RecipeListComponent.inject(me.androidbox.busbybaking.recipieslist.RecipeListView)] me.androidbox.busbybaking.recipieslist.MainActivity cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. 
me.androidbox.busbybaking.recipieslist.MainActivity is injected at 
me.androidbox.busbybaking.recipieslist.RecipeListView.mainActivity 
me.androidbox.busbybaking.recipieslist.RecipeListView is injected at 
me.androidbox.busbybaking.di.RecipeListComponent.inject(target) 

非常感謝您的任何建議。

+2

難道你不使用'getActivity()'獲得片段中的活動嗎?修復工作的 – EpicPandaForce

回答

1

如果你看一下你的模塊

@RecipeListScope 
@Provides 
public RecipeItemListener providesRecipeListMainActivity() { 
    return mainActivity; 
} 

您提供的接口(這是好的)而不是MainActivity(實現)。

既然你要求MainActivity

@Inject MainActivity mainActivity; 

您收到此錯誤:

MainActivity cannot be provided [...]

,因爲你只提供RecipeItemListener


從需要在RecipeListViewMainActivity切換你的代碼

@Inject RecipeItemListener recipeListener; 

,它應該工作,如果你設置的其餘部分是正確的。

+0

。我想要插入的原因是我想調用MainActivity中的方法,該方法從Activity所託管的片段實現接口。我想我可以將Activity傳遞給onAttach方法中的片段。但是,我認爲注射是更好的方法。 – ant2009

0

您可以在片段使用getActivity(訪問活動),並將其投放到您的接口聽衆這樣

((RecipeItemListener)getActivity()).doSomethingOnListener() 

簡單得多,沒有任何不必要的注射