如何保存来自来另一个活动数据 [英] How to save data from come another Activity

查看:92
本文介绍了如何保存来自来另一个活动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存与共享preference.This数据来自另一个活动数据。

I want to save data with sharedpreference.This data come from another Activity.

推荐答案

写入共享preferences

要写入共享preferences文件,通过调用编辑()在您的共享preferences创建共享preferences.Editor。

To write to a shared preferences file, create a SharedPreferences.Editor by calling edit() on your SharedPreferences.

传递要与方法,如putInt()和putString()写入键和值。然后调用commit()保存更改。例如:

Pass the keys and values you want to write with methods such as putInt() and putString(). Then call commit() to save the changes. For example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

从共享preferences阅读

要检索的共享preferences文件值,调用方法,如调用getInt()和GetString(),提供关键你想要的值,和可选的默认值返回,如果该键不在present。例如:

To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

这篇关于如何保存来自来另一个活动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆