Last active
August 24, 2018 09:06
-
-
Save mengdd/70970eb8af66d11933c1ad2869c832b2 to your computer and use it in GitHub Desktop.
Dagger Android basic classes sample code in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dagger.Module; | |
| import dagger.android.ContributesAndroidInjector; | |
| @Module | |
| public abstract class ActivityModule { | |
| @ContributesAndroidInjector(modules = {FragmentBuildersModule.class}) | |
| abstract MainActivity contributeMainActivity(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.app.Application; | |
| import javax.inject.Singleton; | |
| import dagger.BindsInstance; | |
| import dagger.Component; | |
| import dagger.android.AndroidInjectionModule; | |
| import dagger.android.support.AndroidSupportInjectionModule; | |
| @Singleton | |
| @Component(modules = { | |
| AndroidInjectionModule.class, | |
| AndroidSupportInjectionModule.class, | |
| AppModule.class, | |
| ActivityModule.class | |
| }) | |
| public interface AppComponent { | |
| @Component.Builder | |
| interface Builder { | |
| @BindsInstance | |
| AppComponent.Builder application(Application application); | |
| AppComponent build(); | |
| } | |
| void inject(MyApplication application); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.app.Activity; | |
| import android.app.Application; | |
| import android.os.Bundle; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.app.FragmentActivity; | |
| import android.support.v4.app.FragmentManager; | |
| import dagger.android.AndroidInjection; | |
| import dagger.android.support.AndroidSupportInjection; | |
| import dagger.android.support.HasSupportFragmentInjector; | |
| public class AppInjector { | |
| public static void init(MyApplication application) { | |
| DaggerAppComponent.builder() | |
| .application(application) | |
| .build() | |
| .inject(application); | |
| application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { | |
| @Override | |
| public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | |
| handleActivity(activity); // calledn after Activity's super.onCreate() invoked, so anything in onCreate() is injected. | |
| } | |
| @Override | |
| public void onActivityStarted(Activity activity) { | |
| } | |
| @Override | |
| public void onActivityResumed(Activity activity) { | |
| } | |
| @Override | |
| public void onActivityPaused(Activity activity) { | |
| } | |
| @Override | |
| public void onActivityStopped(Activity activity) { | |
| } | |
| @Override | |
| public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | |
| } | |
| @Override | |
| public void onActivityDestroyed(Activity activity) { | |
| } | |
| }); | |
| } | |
| private static void handleActivity(Activity activity) { | |
| if (activity instanceof HasSupportFragmentInjector) { | |
| AndroidInjection.inject(activity); | |
| } | |
| if (activity instanceof FragmentActivity) { | |
| ((FragmentActivity) activity).getSupportFragmentManager() | |
| .registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() { | |
| @Override | |
| public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) { | |
| super.onFragmentCreated(fm, f, savedInstanceState); | |
| // called after Fragment's onCreate(), so the dependencies are still null in onCreate() | |
| // If you need some work in onCreate(), just add an inject() method in component and call it Fragment's onCreate() | |
| if (f instanceof Injectable) { | |
| AndroidSupportInjection.inject(f); | |
| } | |
| } | |
| }, true); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dagger.Module; | |
| import dagger.android.ContributesAndroidInjector; | |
| @Module | |
| public abstract class FragmentBuildersModule { | |
| @ContributesAndroidInjector | |
| abstract HomeFragment contributeHomeFragment(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment