sharedpreferences

android Jetpack

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

保存简单数据,数据的持久化。


提示:以下是本篇文章正文内容,下面案例可供参考

一、sharedpreferences?

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、使用步骤

1.getPreferences

代码如下(示例):

//SharedPreferences shp = getSharedPreferences("myShared", Context.MODE_PRIVATE);//适合多个Activity共享数据
SharedPreferences shp = getPreferences(Context.MODE_PRIVATE);//默认使用了activity的名字
SharedPreferences.Editor edit = shp.edit();
edit.putInt("NUM", 100);
edit.apply();

int x = shp.getInt("NUM",10);

2.model中使用

代码如下(示例):
在这里插入图片描述

Activity中初始化该MyData:
在这里插入图片描述

3.在ViewModel中使用

MainViewModel:

public class MainViewModel extends AndroidViewModel {
    final static private String KEY = "MY_KEY";
    public static final String MAIN_VIEW_MODEL = "MainViewModel";
    private SavedStateHandle handle;
    private final SharedPreferences shp = getApplication().getSharedPreferences(MAIN_VIEW_MODEL, Context.MODE_PRIVATE);
    // TODO: Implement the ViewModel
//    private MutableLiveData<Integer> counter;

    public MainViewModel(@NonNull @NotNull Application application,SavedStateHandle handle) {
        super(application);
        this.handle = handle;
        if (!handle.contains(KEY)) {
            int key = load();
            handle.set(KEY, key);
        }
    }

    public MutableLiveData<Integer> getCount() {
        return handle.getLiveData(KEY);
    }

    private int load(){
        int key = shp.getInt(KEY, 0);
        return key;
    }

    /**
     * Activity的onPause中调用,为了防止强制杀死
     */
    public void save() {
        Integer key = getCount().getValue();
        SharedPreferences.Editor edit = shp.edit();
        edit.putInt(KEY, key);
        edit.apply();
    }

    public void addCount(int num) {
//        int count = counter.getValue();
//        counter.setValue(count + num);
//        getCount().setValue(getCount().getValue() + num);
        handle.set(KEY,getCount().getValue()+num);
    }
}

MainFragment:

public class MainFragment extends Fragment {

    private MainViewModel mViewModel;
    private MainFragmentBinding binding;

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.main_fragment,container,false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this,new SavedStateViewModelFactory(getActivity().getApplication(), this)).get(MainViewModel.class);
        // TODO: Use the ViewModel
        binding.setMainViewModel(mViewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        mViewModel.save();
    }
}

该处使用的url网络请求的数据。


总结

  1. getPreferences和getSharedPreferences只是data.data.包中xml文件名的区别。
  2. AndroidViewModel 该父类需要构造里有Application application。
  3. 自有model类中使用SharedPreferences需要在构造时传入Context或Application。
  4. 为了避免频繁调用save方法,持久化存储的动作放在onPause函数中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值