Dagger2 record, dagger2 record

Source: Internet
Author: User

Dagger2 record, dagger2 record

As a code farmer, you don't need to talk about anything.

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 (); extends fithelper (); // provides http help class}View Code
AppModule
@ Modulepublic class AppModule {private App application; public AppModule (App application) {this. application = application ;}@ Provides @ Singleton @ ContextLife ("Application") public App provideApplication () {return application ;}@ Provides @ Singleton public login fithelper provideRetrofitHelper () {return new response fithelper ();}}View Code
ActivityComponent
@ ActivityScope @ Component (dependencies = AppComponent. class, modules = ActivityModule. class) // Associate public interface ActivityComponent {Activity getActivity (); void inject (LoginActivity loginActivity); // use the void inject (MainActivity mainActivity) function in the class );}View Code
ActivityModule
@ Modulepublic class ActivityModule {private Activity mActivity; public ActivityModule (Activity activity) {this. mActivity = activity;} @ Provides @ ActivityScope public Activity provideActivity () {return mActivity;} @ Provides @ ActivityScope public IPresenterFactory providePresenter (Response fithelper response fithelper) {return new PresenterFactory (Response fithelper );}}View Code

Use injection in the Base; otherwise, the injection will fail.

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

Now all the prerequisites are available.

Click Run to automatically generate the injection code (no problem)

Use injection:

Public class MainActivity extends PresenterActivity {@ Inject protected IPresenterFactory mPresenter; // no object is created. inject label injection type private final int injection = 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); // If the injection is successful, success will be printed.} @ 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 ("Refresh UI", context );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.