网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
public interface SaladComponent {
.....
//这里代码不用动
}
```
```
* 在Salad里面注入tomato,别忘了“tomatoComponent(tomatoComponent)”
```
public class Salad {
@Inject
Tomato tomato;
......
public Salad() {
// 这里必须先得到TomatoComponent
TomatoComponent tomatoComponent = DaggerTomatoComponent.builder().tomatoModule(new TomatoModule()).build();
// 在这里通过传入“tomatoComponent(tomatoComponent)”构建SaladComponent,
SaladComponent saladComponent = DaggerSaladComponent.builder().saladModule(new SaladModule()).tomatoComponent(tomatoComponent).build();
saladComponent.inject(this);
......
}
......
}
```
3. 测试,tomato注入成功了
E/TAG: 我是一个西红柿
以下内容摘自Android常用开源工具(2)-Dagger2进阶
除了dependencies还可以使用SubComponent,相当于子父类继承关系
如果一个Component的功能不能满足你的需求,你需要对它进行拓展,一种办法是使用Component(dependencies=××.classs)。另外一种是使用@Subcomponent,Subcomponent用于拓展原有component。同时,这将产生一种副作用——子component同时具备两种不同生命周期的scope。子Component具备了父Component拥有的Scope,也具备了自己的Scope。
Subcomponent其功能效果优点类似component的dependencies。但是使用@Subcomponent不需要在父component中显式添加子component需要用到的对象,只需要添加返回子Component的方法即可,子Component能自动在父Component中查找缺失的依赖。
//父Component:
@PerApp
@Component(modules=××××)
public AppComponent{
SubComponent subcomponent(); //1.只需要在父Component添加返回子Component的方法即可
}
//子Component:
@PerAcitivity //2.注意子Component的Scope范围小于父Component
@Subcomponent(modules=××××) //3.使用@Subcomponent
public SubComponent{
void inject(SomeActivity activity);
}
//使用
public class SomeActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
...
App.getComponent().subCpmponent().inject(this);//4.调用subComponent方法创建出子Component
}
}
通过Subcomponent,子Component就好像同时拥有两种Scope,当注入的元素来自父Component的Module,则这些元素会缓存在父Component,当注入的元素来自子Component的Module,则这些元素会缓存在子Component中。
还有一个Lazy,懒加载,用于延迟加载
Lazy和Provider都是用于包装Container中需要被注入的类型,Lazy用于延迟加载,Provide用于强制重新加载,具体如下:
public class Container{
@Inject Lazy<Fruit> lazyFruit; //注入Lazy元素
@Inject Provider<Fruit> providerFruit; //注入Provider元素
public void init(){
DaggerComponent.create().inject(this);
Fruit f1=lazyFruit.get(); //在这时才创建f1,以后每次调用get会得到同一个f1对象
Fruit f2=providerFruit.get(); //在这时创建f2,以后每次调用get会再强制调用Module的Provides方法一次,根据Provides方法具体实现的不同,可能返回跟f2是同一个对象,也可能不是。
}
}
值得注意的是,Provider保证每次重新加载,但是并不意味着每次返回的对象都是不同的。只有Module的Provide方法每次都创建新实例时,Provider每次get()的对象才不相同。
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!