android获取全局context,Android获取全局Context的方法

Android获取全局Context的方法

如何构建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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值