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<Favorites> 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<Object>() {
@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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM