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 listData = mFavoritesDBManager.getFavoritesListData();
}
=====================
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() {
@Override
public void onNext(Object o) {
ToastUtil.showLongToast("get favorites ");
String tradingPair = data.getQuote() + data.getBase();
Favorites fav = new Favorites();
fav.setTradingPair(tradingPair);
mFavoritesDBManager.addFavoritesData(fav);
}
@Override
public void onError(Throwable e) {
ToastUtil.showLongToast("get favorites error");
}
}, fragment.bindToLifecycle(),false,false);

更多相关文章

  1. Android常用三方框架
  2. android框架结构
  3. Stagefright框架解读(—)音视频Playback流程
  4. 整理 酷炫 Android(安卓)开源UI框架 FAB
  5. Android(安卓)异步 RxAndroid框架官方例子
  6. QMUI android 框架 git下载项目运行报错解决
  7. 整理 酷炫 Android、Flutter 开源UI框架 Toast、Dialog
  8. [置顶] Android(安卓)Retrofit 2.0框架上传图片解决方案(一张与多
  9. Android(安卓)--- 应用中全局变量的设置

随机推荐

  1. 创建一个对外界只读的属性,但是我的方法仍
  2. jQuery实用工具集
  3. 一名美工(HTML+CSS及界面设计)对于JavaScri
  4. 扩展子div时如何扩展容器div的高度
  5. 每次在HTML5中加载静态图像
  6. JavaScript异步编程:设计快速响应的网络应
  7. 谷歌映射API v3: InfoWindow大小不正确
  8. 传递变量以便以角度移动子弹
  9. 如何将变量推送到web客户端以获取ajax?
  10. How to find IP address or Location of