Android获取全局Context的方法
Android获取全局Context的方法
Android--应用全局获取Context - 超宇的博客 - CSDN博客
https://blog.csdn.net/chaoyu168/article/details/64444274
如何构建Android MVVM 应用框架 -
https://tech.meituan.com/android_mvvm.html
看这篇文章说的还是需要每个ViewModel需要持了一个Context的引用
但应该可以做个ViewModel内都可以使用的,不用每个方法都传入那么麻烦
使用mvvm框架可以参考下面的实现方式,得改几个地方比较繁琐
=====================
实例:
AppComponent增加 FavoritesDBManager FavoritesDBManager();
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
SharedPreferences sharedPreferences();
Context context();
FavoritesDBManager FavoritesDBManager();
}
---------------------
CoinListComponent增加注解:dependencies = AppComponent.class
@FragmentScope
@Component(modules = CoinListModule.class, dependencies = AppComponent.class)
public interface CoinListComponent {
----------------
全局AppModule增加provideFavoritesDBManager
@Module
public class AppModule {
private Context context;
public AppModule(Context context) {
this.context = context;
}
@Provides
@Singleton
FavoritesDBManager provideFavoritesDBManager() {
return new FavoritesDBManager(context);
}
----------------
CoinListViewModel 增加@Inject FavoritesDBManager mFavoritesDBManager;
用到的的地方直接调用
public class CoinListViewModel extends BaseViewModel implements CoinlistItemClickLisnter {
@Inject
FavoritesDBManager mFavoritesDBManager;
if(status == 0) {
List
}
=====================
java.lang.IllegalStateException: com.aax.exchange.inject.AppComponent must be set
at com.aax.exchange.inject.component.DaggerCoinListComponent$Builder.build(DaggerCoinListComponent.java:75)
at com.aax.exchange.fragment.CoinListFragment.inject(CoinListFragment.java:56)
at com.aax.exchange.base.BaseMvvmFragment.onCreateView(BaseMvvmFragment.java:54)
---------------------
用到的地方CoinSearchActivity,CoinListFragment都要加上.appComponent(ComponentHolder.getComponent())
--------------
@Override
protected void inject() {
CoinSearchComponent component = DaggerCoinSearchComponent.builder()
.coinSearchModule(new CoinSearchModule(this)).appComponent(ComponentHolder.getComponent())
.build();
@Override
protected void inject() {
CoinListComponent component = DaggerCoinListComponent.
builder().coinListModule(new CoinListModule(this)).appComponent(ComponentHolder.getComponent())
.build();
------------------
public class ComponentHolder {
private static AppComponent mComponent;
public static AppComponent getComponent() {
return mComponent;
}
public static void setComponent(AppComponent component) {
mComponent = component;
}
}
=====================
subscribe(fragment.mAct,Api.getApiService().getFavorites(new ObserverResponseListener
更多相关文章
- Android常用三方框架
- android框架结构
- Stagefright框架解读(—)音视频Playback流程
- 整理 酷炫 Android(安卓)开源UI框架 FAB
- Android(安卓)异步 RxAndroid框架官方例子
- QMUI android 框架 git下载项目运行报错解决
- 整理 酷炫 Android、Flutter 开源UI框架 Toast、Dialog
- [置顶] Android(安卓)Retrofit 2.0框架上传图片解决方案(一张与多
- Android(安卓)--- 应用中全局变量的设置