美文网首页
Jetpack之SavedStateHandle应用

Jetpack之SavedStateHandle应用

作者: TMAC_EAH | 来源:发表于2020-12-14 15:08 被阅读0次
1.添加依赖
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.0-beta01"
2.使用参考 (viewmodel)
 public class IHomeFragmentModel extends ViewModel implements ViewModelKey {
        public MutableLiveData<List<GetSlideshowListBean>> mBannerLiveData = new MutableLiveData();
        SavedStateHandle mSavedStateHandle;

        public IHomeFragmentModel(SavedStateHandle mSavedStateHandle) {
            mBannerLiveData = mSavedStateHandle.getLiveData(KEY_BANNER);
           if (mSavedStateHandle.contains(KEY_BANNER)) {
            try {
                Log.e("复活", "复活数据=" + mBannerLiveData.getValue().size());
            } catch (Exception e) {
                e.printStackTrace();
            }
          } else {
            Log.e("复活", "当前无复活数据=");
        }
            this.mSavedStateHandle = mSavedStateHandle;
        }

        public boolean tryRestoreHistoryDataUnSucess(MutableLiveData target, String key) {
            if (mSavedStateHandle.contains(key)) {
                mSavedStateHandle.remove(key); // 用一次
                if (target != null && target.getValue() == null) {
                    return true;
                }
                return false;
            }
            return true;
        }

        public void execBannerHttpRequest() {
            if (tryRestoreHistoryDataUnSucess(mBannerLiveData, KEY_BANNER)) {
                // 项目中用的是 okgo 封装成这鸟样了
                HomeService.getSlideshowList2(new IHttpCallback<List<GetSlideshowListBean>>() {

                    @Override
                    public void onSuccess(List<GetSlideshowListBean> data) {
                        mBannerLiveData.postValue(data);
                    }

                    @Override
                    public void onError(String data) {
                       mBannerLiveData.postValue(null);
                    }
                });
            } else {
                Log.e("复活", "获取历史数据成功了=");
            }
        }
    }
3.定义的key~
public interface ViewModelKey {
    String KEY_BANNER = "key_banner";
    String KEY_GUESSYOULIKE = "key_GuessYouLike";
    String KEY_LIVEHOTS = "key_LiveHots";
    String KEY_LIVERECOMMEND = "key_LiveRecommend";
    String KEY_LATESTCLASS = "key_LatestClass";
    String KEY_WEATHERMONITOR = "key_WeatherMonitor";
    String KEY_WEATHER = "key_Weather";
}
4.activity/fragment调用一致
public class A extends AppCompatActivity {
    IHomeFragmentModel iIHomeFragmentModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iIHomeFragmentModel = new ViewModelProvider(this,
                new SavedStateViewModelFactory(BaseApplication.getInstance(), this)).get(IHomeFragmentModel.class);
        iIHomeFragmentModel.mBannerLiveData.observe(this, new Observer<List<GetSlideshowListBean>>() {
            @Override
            public void onChanged(List<GetSlideshowListBean> data) {
                if (data != null) {
                    // do something~
                } else {
                    // do nothing
                }
            }
        });
    }
}
4.模拟杀死重建数据
      1.应用切换到后台
      2.执行命令 adb shell am kill 你的包名
      3.打开应用
      4.观看日志

正常打开
2020-12-14 14:55:55.975 27835-27835/com.sinochem E/复活: 当前无复活数据=
2020-12-14 14:55:57.603 27835-27835/com.sinochem E/复活: 更新banner数据命中=
切换后台干掉app后
2020-12-14 15:04:09.040 30802-30802/com.sinochem E/复活: 复活数据=1
2020-12-14 15:04:09.558 30802-30802/com.sinochem E/复活: 更新banner数据命中=

5.这样应用被杀死,数据被还原就完成,果然是真香~
6.bundle 能承受我的大量数据吗?说实话 我不清楚.....请大佬告知~

相关文章

网友评论

      本文标题:Jetpack之SavedStateHandle应用

      本文链接:https://www.haomeiwen.com/subject/zaqxiktx.html