作為一個碼農,什麼都不用多講,貼代碼 build project build module Application public class App extends Application { private AppComponent appComponent; @Override public v ...
作為一個碼農,什麼都不用多講,貼代碼
build project
build module
Application
public class App extends Application { private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent= DaggerAppComponent.builder() .appModule(new AppModule(this)) .build(); } public AppComponent getAppComponent() { return appComponent; } }View Code
AppComponent
@Singleton @Component(modules = { AppModule.class }) public interface AppComponent { @ContextLife("Application") App getApplication(); RetrofitHelper retrofitHelper(); //提供http的幫助類 }View Code
AppModule
@Module public class AppModule { private App application; public AppModule(App application){ this.application=application; } @Provides @Singleton @ContextLife("Application") public App provideApplication(){ return application; } @Provides @Singleton public RetrofitHelper provideRetrofitHelper() { return new RetrofitHelper(); } }View Code
ActivityComponent
@ActivityScope @Component(dependencies = AppComponent.class, modules = ActivityModule.class)//關聯上下 public interface ActivityComponent { Activity getActivity(); void inject(LoginActivity loginActivity);//要在類中使用註入功能 void inject(MainActivity mainActivity); }View Code
ActivityModule
@Module public class ActivityModule { private Activity mActivity; public ActivityModule(Activity activity) { this.mActivity = activity; } @Provides @ActivityScope public Activity provideActivity() { return mActivity; } @Provides @ActivityScope public IPresenterFactory providePresenter(RetrofitHelper retrofitHelper) { return new PresenterFactory(retrofitHelper); } }View Code
Base裡面使用註入,否則無法註入
public abstract class PresenterActivity extends BaseActivity { private final static String TAG = "PresenterActivity"; protected abstract void initInject(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initInject(); } protected ActivityComponent getActivityComponent(){ return DaggerActivityComponent.builder() .appComponent(App.getInstance().getAppComponent()) .activityModule(getActivityModule()) .build(); } private ActivityModule getActivityModule() { return new ActivityModule(this); } }View Code
此時前提條件都有了
點擊運行自動生成註入代碼(沒問題的前提)
使用註入:
public class MainActivity extends PresenterActivity { @Inject protected IPresenterFactory mPresenter;//沒有創建對象,inject標簽註入類型 private final int BASIC_PERMISSION_REQUEST_CODE = 100; private TextView username; @Override public void findView() { username = findView(R.id.username); username.setText(App.getInstance().getUserEntity().getName()+" hello"); } @Override public void init() { if (mPresenter != null){ Toasts.showShort("mPresenter success",context);//註入成功會列印出success } } @Override public int layoutResID() { return R.layout.activity_main; } @Override protected void initInject() { getActivityComponent().inject(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent() { Toasts.showShort("刷新UI",context); } }