-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial
@Module
public class MyModule {
@Provides
Object myObject() {
return new Object();
}
}
Note: As you can see, no scope has yet been given to the object. This will be addressed in the following steps.
@SharpComponent(
modules = MyModule.class // array of multiple modules can be provided as {A.class, B.class, ...}
)
public class MyApplication extends Application {
}
After building the project, the following two files will have been automatically generated by DaggerSharpener:
SharpMyApplicationComponent.java -- (Name is formed as: Sharp + Class Name + Component)
@Component
@SharpMyApplicationScope
public interface SharpMyApplicationComponent {
MyApplication inject(MyApplication thisClass);
}
SharpMyApplicationScope.java -- (Name is formed as: Sharp + Class Name + Scope)
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface SharpMyApplicationScope {}
@Module
public class MyModule {
@Provides
@SharpMyApplicationScope
Object myObject() {
return new Object();
}
}
@SharpComponent(
modules = MyModule.class
)
public class MyApplication extends Application {
private SharpMyApplicationComponent sharpComponent;
@Inject
Object injectedObject;
@Override
public void onCreate() {
super.onCreate();
// Name is: Dagger + Sharp + Class Name + Component
sharpComponent = DaggerSharpMyApplicationComponent.builder().build();
sharpComponent.inject(this);
Log.d("MyApplication", "onCreate: " + injectedObject.toString());
}
public SharpMyApplicationComponent getSharpComponent() {
return sharpComponent;
}
}
Note: By running the code and checking logcat, you will see that the injectedObject is non-null. This indicates our object got successfully injected.
@SharpComponent(
dependencies = SharpMyApplicationComponent.class
)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Note: The new Sharp Activity Component depends on SharpMyApplicationComponent
After building, two new files are generated:
SharpMainActivityComponent.java
@Component
@SharpMainActivityScope
public interface SharpMainActivityComponent {
MainActivity inject(MainActivity thisClass);
}
SharpMainActivityScope.java
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface SharpMainActivityScope {}
@SharpComponent(
modules = MyModule.class,
provides = Object.class // Array of dependencies can be provided as {A.class, B.class, etc.}
)
public class MyApplication extends Application {
...
}
@SharpComponent(
sharpDependencies = MyApplication.class // The class(es) annotated with @SharpComponent
)
public class MainActivity extends AppCompatActivity {
@Inject
Object myObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerSharpMainActivityComponent.builder()
.sharpMyApplicationComponent(((MyApplication) getApplication()).getSharpComponent())
.build()
.inject(this);
Log.d("MainActivity", "onCreate: " + myObject.toString());
}
}
Note: By running the code and checking logcat, you will see that injectedObject from MyApplication and myObject from MainActivity are indeed the same instance (as long as you did the step 2.2). If you did not do step 2.2, the instance will be different for the two objects.
Using DaggerSharpener will still allow you to use @Inject
on constructors. Also, the generated scopes can be safely used on the classes which inject constructors.
For some docs, keep on reading below!