Skip to content

Commit 2fbbd82

Browse files
Injecting the Application
1 parent cc0acf3 commit 2fbbd82

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
package com.gregoriopalama.daggerexample;
22

3+
import android.app.Activity;
34
import android.app.Application;
45

6+
import com.gregoriopalama.daggerexample.di.DaggerAppComponent;
7+
8+
import javax.inject.Inject;
9+
10+
import dagger.android.AndroidInjector;
11+
import dagger.android.DispatchingAndroidInjector;
12+
import dagger.android.HasActivityInjector;
13+
514
/**
6-
* This will be the application. For now, it will be empty
15+
* This is the application. It uses the generated DaggerAppComponent, and inject itself.
16+
* It also implements HasActivityInjector, in order to provide the possibility to inject
17+
* activities directly from inside of them.
718
*
819
* @author Gregorio Palamà
920
*/
1021

11-
public class DaggerExampleApplication extends Application {
22+
public class DaggerExampleApplication extends Application implements HasActivityInjector {
23+
@Inject
24+
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
25+
26+
@Override
27+
public void onCreate() {
28+
super.onCreate();
29+
30+
DaggerAppComponent
31+
.builder()
32+
.application(this)
33+
.build()
34+
.inject(this);
35+
}
36+
37+
@Override
38+
public AndroidInjector<Activity> activityInjector() {
39+
return dispatchingAndroidInjector;
40+
}
1241
}

0 commit comments

Comments
 (0)