Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> dagger2記錄篇,dagger2記錄

dagger2記錄篇,dagger2記錄

編輯:關於android開發

dagger2記錄篇,dagger2記錄


作為一個碼農,什麼都不用多講,貼代碼

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);
    }

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved