Forked from vestrel00/Dagger2SimpleExample.java
Last active
September 26, 2018 17:44
-
-
Save ebraminio/81d2d8e868335683d4bc870dd35d990e to your computer and use it in GitHub Desktop.
A: Dagger.android 2.11 simple example with support for Singleton, PerActivity, PerFragment, and PerChildFragment scopes
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
// This is a super simplified example of how to use the new dagger.android framework | |
// Forked from https://gist.github.com/vestrel00/64be913f954989fe52c674247e093218/ | |
// but changed to have less boilerplate using the new features. | |
// Used on https://github.com/ebraminio/DroidPersianCalendar also | |
// App.java | |
public class App extends DaggerApplication { | |
@Override | |
protected AndroidInjector<? extends DaggerApplication> applicationInjector() { | |
return DaggerAppComponent.builder().create(this); | |
} | |
} | |
// AppModule.java | |
@Module(includes = AndroidInjectionModule.class) | |
abstract class AppModule { | |
@PerActivity | |
@ContributesAndroidInjector(modules = MainActivityModule.class) | |
abstract MainActivity mainActivityInjector(); | |
} | |
// AppComponent.java | |
@Singleton | |
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class}) | |
public interface AppComponent extends AndroidInjector<App> { | |
@Component.Builder | |
abstract class Builder extends AndroidInjector.Builder<App> { | |
} | |
} | |
// MainActivity.java | |
public final class MainActivity extends DaggerActivity { | |
@Inject | |
AppDependency appDependency; // same object from App | |
@Inject | |
ActivityDependency activityDependency; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
if (savedInstanceState == null) { | |
addFragment(R.id.fragment_container, new MainFragment()); | |
} | |
} | |
private final void addFragment(@IdRes int containerViewId, Fragment fragment) { | |
getFragmentManager.beginTransaction() | |
.add(containerViewId, fragment) | |
.commit(); | |
} | |
} | |
// MainActivityModule.java | |
@Module | |
public abstract class MainActivityModule { | |
@PerFragment | |
@ContributesAndroidInjector(modules = MainFragmentModule.class) | |
abstract MainFragment mainFragmentInjector(); | |
} | |
// MainFragment.java | |
// Could instead extend DialogFragment to add DialogFragment capabilities. | |
// DialogFragments may be embedded as regular fragments in a view of an Activity or Fragment | |
// and may also be shown as a dialog or in an alert dialog. | |
public final class MainFragment extends DaggerFragment { | |
@Inject | |
AppDependency appDependency; // same object from App | |
@Inject | |
ActivityDependency activityDependency; // same object from MainActivity | |
@Inject | |
FragmentDependency fragmentDependency; | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, | |
Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.main_fragment, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
if (savedInstanceState == null) { | |
addChildFragment(R.id.child_fragment_container, new MainChildFragment()); | |
} | |
} | |
private final void addChildFragment(@IdRes int viewId, Fragment fragment) { | |
getChildFragmentManager() | |
.beginTransaction() | |
.add(viewId, fragment) | |
.commit(); | |
} | |
} | |
// MainFragmentModule.java | |
@Module | |
public abstract class MainFragmentModule { | |
@PerChildFragment | |
@ContributesAndroidInjector(modules = MainChildFragmentModule.class) | |
abstract MainChildFragment mainChildFragmentInjector(); | |
} | |
// MainChildFragment.java | |
public final class MainChildFragment extends DaggerFragment { | |
@Inject | |
AppDependency appDependency; // same object from App | |
@Inject | |
ActivityDependency activityDependency; // same object from MainActivity | |
@Inject | |
FragmentDependency fragmentDependency; // same object from MainFragment | |
@Inject | |
ChildFragmentDependency childFragmentDependency; | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, | |
Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.main_child_fragment, container, false); | |
} | |
} | |
// MainChildFragmentModule.java | |
@Module | |
public abstract class MainChildFragmentModule { | |
} | |
// PerActivity.java | |
@Scope | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface PerActivity { | |
} | |
// PerFragment.java | |
@Scope | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface PerFragment { | |
} | |
// PerChildFragment.java | |
@Scope | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface PerChildFragment { | |
} | |
// AppDependency.java | |
@Singleton | |
public final class AppDependency { | |
@Inject | |
AppDependency() { | |
} | |
} | |
// ActivityDependency.java | |
@PerActivity | |
public final class ActivityDependency { | |
@Inject | |
ActivityDependency() { | |
} | |
} | |
// FragmentDependency.java | |
@PerFragment | |
public final class FragmentDependency { | |
@Inject | |
FragmentDependency() { | |
} | |
} | |
// ChildFragmentDependency.java | |
@PerChildFragment | |
public final class ChildFragmentDependency { | |
@Inject | |
ChildFragmentDependency() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment