Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ahmadronaghdev/cf3295d6ba1d7593be8f1d11d008711a to your computer and use it in GitHub Desktop.

Select an option

Save ahmadronaghdev/cf3295d6ba1d7593be8f1d11d008711a to your computer and use it in GitHub Desktop.

Revisions

  1. @vestrel00 vestrel00 revised this gist Jan 10, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Dagger2SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,10 @@
    // introduced in Dagger 2.10. For a more complete, in-depth guide to dagger.android
    // read https://proandroiddev.com/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd

    // For a complete codebase using dagger.android 2.11/2.12/2.13, butterknife 8.7/8.8, and MVP,
    // For a complete codebase using dagger.android 2.11/2.12/2.13/2.14, butterknife 8.7/8.8, and MVP,
    // see https://github.com/vestrel00/android-dagger-butterknife-mvp

    // This example works with Dagger 2.11/2.12/2.13. Starting with Dagger 2.11,
    // This example works with Dagger 2.11/2.12/2.13/2.14. Starting with Dagger 2.11,
    // @ContributesAndroidInjector was introduced removing the need to define @Subcomponent classes.

    // App.java
  2. @vestrel00 vestrel00 revised this gist Dec 19, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Dagger2SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -85,6 +85,9 @@ public abstract class MainActivityModule {

    // MainFragment.java
    // Could also extend DaggerFragment instead of implementing HasFragmentInjector
    // 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 Fragment implements HasFragmentInjector {

    @Inject
  3. @vestrel00 vestrel00 revised this gist Nov 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Dagger2SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -103,7 +103,7 @@ public final class MainFragment extends Fragment implements HasFragmentInjector
    @Override
    public void onAttach(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    // Perform injection here for versions before M onAttach(*Context*) did not yet exist
    // Perform injection here for versions before M as onAttach(*Context*) did not yet exist
    AndroidInjection.inject(this);
    }
    super.onAttach(activity);
  4. @vestrel00 vestrel00 revised this gist Nov 29, 2017. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions Dagger2SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,11 @@
    // For a complete codebase using dagger.android 2.11/2.12/2.13, butterknife 8.7/8.8, and MVP,
    // see https://github.com/vestrel00/android-dagger-butterknife-mvp

    // Note that this example works with Dagger 2.11/2.12/2.13. Starting with Dagger 2.11,
    // This example works with Dagger 2.11/2.12/2.13. Starting with Dagger 2.11,
    // @ContributesAndroidInjector was introduced removing the need to define @Subcomponent classes.

    // Could also extend DaggerApplication instead of implementing HasActivityInjector
    // App.java
    // Could also extend DaggerApplication instead of implementing HasActivityInjector
    public class App extends Application implements HasActivityInjector {

    @Inject
    @@ -45,8 +45,8 @@ interface AppComponent {
    void inject(App app);
    }

    // Could also extend DaggerActivity instead of implementing HasFragmentInjector
    // MainActivity.java
    // Could also extend DaggerActivity instead of implementing HasFragmentInjector
    public final class MainActivity extends Activity implements HasFragmentInjector {

    @Inject
    @@ -83,8 +83,8 @@ public abstract class MainActivityModule {
    abstract MainFragment mainFragmentInjector();
    }

    // Could also extend DaggerFragment instead of implementing HasFragmentInjector
    // MainFragment.java
    // Could also extend DaggerFragment instead of implementing HasFragmentInjector
    public final class MainFragment extends Fragment implements HasFragmentInjector {

    @Inject
    @@ -103,8 +103,7 @@ public final class MainFragment extends Fragment implements HasFragmentInjector
    @Override
    public void onAttach(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    // Perform injection here before M, L (API 22) and below because onAttach(Context)
    // is not yet available at L.
    // Perform injection here for versions before M onAttach(*Context*) did not yet exist
    AndroidInjection.inject(this);
    }
    super.onAttach(activity);
    @@ -113,7 +112,7 @@ public void onAttach(Activity activity) {
    @Override
    public void onAttach(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    // Perform injection here for M (API 23) due to deprecation of onAttach(Activity).
    // Perform injection here for M (API 23) due to deprecation of onAttach(*Activity*).
    AndroidInjection.inject(this);
    }
    super.onAttach(context);
  5. @vestrel00 vestrel00 revised this gist Nov 8, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Dagger2SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,11 @@
    // introduced in Dagger 2.10. For a more complete, in-depth guide to dagger.android
    // read https://proandroiddev.com/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd

    // For a complete codebase using dagger.android 2.11, butterknife 8.7, and MVP,
    // For a complete codebase using dagger.android 2.11/2.12/2.13, butterknife 8.7/8.8, and MVP,
    // see https://github.com/vestrel00/android-dagger-butterknife-mvp

    // Note that this example uses Dagger 2.11, where @ContributesAndroidInjector was
    // introduced removing the need to define @Subcomponent classes.
    // Note that this example works with Dagger 2.11/2.12/2.13. Starting with Dagger 2.11,
    // @ContributesAndroidInjector was introduced removing the need to define @Subcomponent classes.

    // Could also extend DaggerApplication instead of implementing HasActivityInjector
    // App.java
  6. @vestrel00 vestrel00 revised this gist Aug 14, 2017. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion Dagger2SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -99,9 +99,23 @@ public final class MainFragment extends Fragment implements HasFragmentInjector
    @Inject
    DispatchingAndroidInjector<Fragment> childFragmentInjector;

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    // Perform injection here before M, L (API 22) and below because onAttach(Context)
    // is not yet available at L.
    AndroidInjection.inject(this);
    }
    super.onAttach(activity);
    }

    @Override
    public void onAttach(Context context) {
    AndroidInjection.inject(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    // Perform injection here for M (API 23) due to deprecation of onAttach(Activity).
    AndroidInjection.inject(this);
    }
    super.onAttach(context);
    }

  7. @vestrel00 vestrel00 renamed this gist Aug 2, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @vestrel00 vestrel00 revised this gist Jul 31, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // This is a super simplified example of how to use the new dagger.android framework
    // introduced in Dagger 2.10. For a more complete, in-depth guide to dagger.android
    // read https://medium.com/@vestrel00/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd
    // read https://proandroiddev.com/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd

    // For a complete codebase using dagger.android 2.11, butterknife 8.7, and MVP,
    // see https://github.com/vestrel00/android-dagger-butterknife-mvp
  9. @vestrel00 vestrel00 revised this gist Jul 28, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    // introduced in Dagger 2.10. For a more complete, in-depth guide to dagger.android
    // read https://medium.com/@vestrel00/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd

    // For a complete codebase using dagger.android 2.11, butterknife 8.7, and MVP,
    // see https://github.com/vestrel00/android-dagger-butterknife-mvp

    // Note that this example uses Dagger 2.11, where @ContributesAndroidInjector was
    // introduced removing the need to define @Subcomponent classes.

  10. @vestrel00 vestrel00 revised this gist Jul 28, 2017. No changes.
  11. @vestrel00 vestrel00 revised this gist Jul 28, 2017. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    // Note that this example uses Dagger 2.11, where @ContributesAndroidInjector was
    // introduced removing the need to define @Subcomponent classes.

    // Could also extend DaggerApplication instead of implementing HasActivityInjector
    // App.java
    public class App extends Application implements HasActivityInjector {

    @@ -41,11 +42,12 @@ interface AppComponent {
    void inject(App app);
    }

    // Could also extend DaggerActivity instead of implementing HasFragmentInjector
    // MainActivity.java
    public final class MainActivity extends Activity implements HasFragmentInjector {

    @Inject
    AppDependency appDependency;
    AppDependency appDependency; // same object from App

    @Inject
    ActivityDependency activityDependency;
    @@ -78,17 +80,18 @@ public abstract class MainActivityModule {
    abstract MainFragment mainFragmentInjector();
    }

    // Could also extend DaggerFragment instead of implementing HasFragmentInjector
    // MainFragment.java
    public final class MainFragment extends Fragment implements HasFragmentInjector {

    @Inject
    AppDependency appDependency;
    AppDependency appDependency; // same object from App

    @Inject
    ActivityDependency activityDependency;
    ActivityDependency activityDependency; // same object from MainActivity

    @Inject
    FragmentDependency fragmentDepency;
    FragmentDependency fragmentDepency;

    @Inject
    DispatchingAndroidInjector<Fragment> childFragmentInjector;
    @@ -132,13 +135,13 @@ public abstract class MainFragmentModule {
    public final class MainChildFragment extends Fragment {

    @Inject
    AppDependency appDependency;
    AppDependency appDependency; // same object from App

    @Inject
    ActivityDependency activityDependency;
    ActivityDependency activityDependency; // same object from MainActivity

    @Inject
    FragmentDependency fragmentDepency;
    FragmentDependency fragmentDepency; // same object from MainFragment

    @Inject
    ChildFragmentDependency childFragmentDepency;
  12. @vestrel00 vestrel00 revised this gist Jul 28, 2017. 1 changed file with 32 additions and 33 deletions.
    65 changes: 32 additions & 33 deletions SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -41,14 +41,6 @@ interface AppComponent {
    void inject(App app);
    }

    // AppDependency.java
    @Singleton
    public final class AppDependency {
    @Inject
    AppDependency() {
    }
    }

    // MainActivity.java
    public final class MainActivity extends Activity implements HasFragmentInjector {

    @@ -86,14 +78,6 @@ public abstract class MainActivityModule {
    abstract MainFragment mainFragmentInjector();
    }

    // ActivityDependency.java
    @PerActivity
    public final class ActivityDependency {
    @Inject
    ActivityDependency() {
    }
    }

    // MainFragment.java
    public final class MainFragment extends Fragment implements HasFragmentInjector {

    @@ -144,14 +128,6 @@ public abstract class MainFragmentModule {
    abstract MainChildFragment mainChildFragmentInjector();
    }

    // FragmentDependency.java
    @PerFragment
    public final class FragmentDependency {
    @Inject
    FragmentDependency() {
    }
    }

    // MainChildFragment.java
    public final class MainChildFragment extends Fragment {

    @@ -185,14 +161,6 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
    public abstract class MainChildFragmentModule {
    }

    // ChildFragmentDependency.java
    @PerChildFragment
    public final class ChildFragmentDependency {
    @Inject
    ChildFragmentDependency() {
    }
    }

    // PerActivity.java
    @Scope
    @Retention(RetentionPolicy.RUNTIME)
    @@ -210,4 +178,35 @@ public final class ChildFragmentDependency {
    @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() {
    }
    }
  13. @vestrel00 vestrel00 created this gist Jul 28, 2017.
    213 changes: 213 additions & 0 deletions SimpleExample.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,213 @@
    // This is a super simplified example of how to use the new dagger.android framework
    // introduced in Dagger 2.10. For a more complete, in-depth guide to dagger.android
    // read https://medium.com/@vestrel00/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd

    // Note that this example uses Dagger 2.11, where @ContributesAndroidInjector was
    // introduced removing the need to define @Subcomponent classes.

    // App.java
    public class App extends Application implements HasActivityInjector {

    @Inject
    AppDependency appDependency;

    @Inject
    DispatchingAndroidInjector<Activity> activityInjector;

    @Override
    public void onCreate() {
    super.onCreate();
    DaggerAppComponent.create().inject(this);
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
    return activityInjector;
    }
    }

    // AppModule.java
    @Module(includes = AndroidInjectionModule.class)
    abstract class AppModule {
    @PerActivity
    @ContributesAndroidInjector(modules = MainActivityModule.class)
    abstract MainActivity mainActivityInjector();
    }

    // AppComponent.java
    @Singleton
    @Component(modules = AppModule.class)
    interface AppComponent {
    void inject(App app);
    }

    // AppDependency.java
    @Singleton
    public final class AppDependency {
    @Inject
    AppDependency() {
    }
    }

    // MainActivity.java
    public final class MainActivity extends Activity implements HasFragmentInjector {

    @Inject
    AppDependency appDependency;

    @Inject
    ActivityDependency activityDependency;

    @Inject
    DispatchingAndroidInjector<Fragment> fragmentInjector;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    AndroidInjection.inject(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    if (savedInstanceState == null) {
    addFragment(R.id.fragment_container, new MainFragment());
    }
    }

    @Override
    public final AndroidInjector<Fragment> fragmentInjector() {
    return fragmentInjector;
    }
    }

    // MainActivityModule.java
    @Module
    public abstract class MainActivityModule {
    @PerFragment
    @ContributesAndroidInjector(modules = MainFragmentModule.class)
    abstract MainFragment mainFragmentInjector();
    }

    // ActivityDependency.java
    @PerActivity
    public final class ActivityDependency {
    @Inject
    ActivityDependency() {
    }
    }

    // MainFragment.java
    public final class MainFragment extends Fragment implements HasFragmentInjector {

    @Inject
    AppDependency appDependency;

    @Inject
    ActivityDependency activityDependency;

    @Inject
    FragmentDependency fragmentDepency;

    @Inject
    DispatchingAndroidInjector<Fragment> childFragmentInjector;

    @Override
    public void onAttach(Context context) {
    AndroidInjection.inject(this);
    super.onAttach(context);
    }

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

    @Override
    public final AndroidInjector<Fragment> fragmentInjector() {
    return childFragmentInjector;
    }
    }

    // MainFragmentModule.java
    @Module
    public abstract class MainFragmentModule {
    @PerChildFragment
    @ContributesAndroidInjector(modules = MainChildFragmentModule.class)
    abstract MainChildFragment mainChildFragmentInjector();
    }

    // FragmentDependency.java
    @PerFragment
    public final class FragmentDependency {
    @Inject
    FragmentDependency() {
    }
    }

    // MainChildFragment.java
    public final class MainChildFragment extends Fragment {

    @Inject
    AppDependency appDependency;

    @Inject
    ActivityDependency activityDependency;

    @Inject
    FragmentDependency fragmentDepency;

    @Inject
    ChildFragmentDependency childFragmentDepency;

    @Override
    public void onAttach(Context context) {
    AndroidInjection.inject(this);
    super.onAttach(context);
    }

    @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 {
    }

    // ChildFragmentDependency.java
    @PerChildFragment
    public final class ChildFragmentDependency {
    @Inject
    ChildFragmentDependency() {
    }
    }

    // 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 {
    }