Setting Default Values


The preferences you create probably define some important behaviors for your application, so it's necessary that you initialize the associated SharedPreferences file with default values for each Preference when the user first opens your application.

The first thing you must do is specify a default value for each Preference object in your XML file using theandroid:defaultValue attribute. The value can be any data type that is appropriate for the correspondingPreference object. For example:

<!-- default value is a boolean -->
<CheckBoxPreference
android:defaultValue="true"
... /> <!-- default value is a string -->
<ListPreference
android:defaultValue="@string/pref_syncConnectionTypes_default"
... />

Then, from the onCreate() method in your application's main activity—and in any other activity through which the user may enter your application for the first time—call setDefaultValues():

PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

Calling this during onCreate() ensures that your application is properly initialized with default settings, which your application might need to read in order to determine some behaviors (such as whether to download data while on a cellular network).

This method takes three arguments:

  • Your application Context.
  • The resource ID for the preference XML file for which you want to set the default values.
  • A boolean indicating whether the default values should be set more than once.

    When false, the system sets the default values only if this method has never been called in the past (or theKEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false).

As long as you set the third argument to false, you can safely call this method every time your activity starts without overriding the user's saved preferences by resetting them to the defaults. However, if you set it to true, you will override any previous values with the defaults.

Android偏好设置(4)设置默认值的更多相关文章

  1. 设置easyui input默认值

    /*设置input 焦点*/ $(function () { //集体调用 $(".formTextBoxes input").each(function () { $(this) ...

  2. 二货Mysql中设置字段的默认值问题

    Mysql设置字段的默认值的确很落伍 1.不支持函数 2.只支持固定常量. 经常用到的日期类型,因为不支持getdate或者now函数,所以只能设置timestamp类型 而且还必须在默认值那个地方写 ...

  3. mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错

    问题:mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错: ERROR 1067 (42000): Invalid default value for 'u ...

  4. Odoo14 设置Binary字段默认值

    1 # Odoo 中的附件也就是Binary字段都是经过特殊处理的 2 # 首先是上传的时候会进行base64编码后再上传到服务器 3 # 服务器进行压缩存放在odoo文件仓库中 4 # 每个odoo ...

  5. MySQL设置字段的默认值为当前系统时间

    问题产生: 当我们在对某个字段进行设置时间默认值,该默认值必须是的当前记录的插入时间,那么就将当前系统时间作为该记录创建的时间. 应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,应该由数据 ...

  6. [转]Hibernate设置时间戳的默认值和更新时间的自动更新

    原文地址:http://blog.csdn.net/sushengmiyan/article/details/50360451 Generated and default property value ...

  7. jquery 设置select的默认值

    <select id="sel" > <option value="s1" > aaaa </option> <opt ...

  8. Hibernate设置时间戳的默认值和更新时间的自动更新

    Generated and default property values 生成的和默认的属性值 The database sometimes generates a property value, ...

  9. oracle数据库的一个表中,怎么设置字段的默认值

    如果表已经存在,用如下方法设置默认值. alter table 表名 modify 字段名 default 默认值; 如test表中设置address字段为'浙江省',可用如下语句: alter ta ...

  10. mysql使用default来设置字段的默认值

    mysql创建表时,我们可以使用default来设置表字段的默认值,这样当向表中插入或添加数据时,如果没有为此字段设置任何值,则使用default默认值来填充该字段的值 在使用create table ...

随机推荐

  1. nginx access.log 忽略favicon.ico訪问记录的方法

    favicon.ico 文件是浏览器收藏网址时显示的图标,当第一次訪问页面时.浏览器会自己主动发起请求获取页面的favicon.ico文件.当/favicon.ico文件不存在时,服务器会记录404日 ...

  2. Registration system

    Registration system 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 A new e-mail service "Berlandesk&q ...

  3. &lt;Android&gt;greenrobot-EventBus,guava-Event Bus的异步实现

    刚開始是从otto入手,可是otto不支持异步运行.所以后来才開始研究了Event Bus.关于Event Bus,先前搜索的时候,看到网上的实例,非常碎,并且非常多都是一样的内容,代码看下来基本上是 ...

  4. JSONObjectWithData方法里options參数选择解释

    NSJSONReadingMutableContainers  Specifies that arrays and dictionaries are created as mutable object ...

  5. is和==的区别,小数据池,编码

    1   is  和  == 的区别 1>    id( )表示我们可以通过它来查到在内存中的地址 s = "alex" lst = [1,2, 4] lst = [1, 2, ...

  6. form标签中id和name属性的区别

    HTML元素的ID和Name属性的区别 一直认为ID和NAME是一样的,两个又可以一起出现,甚是疑惑. 今天BAIDU了一下,才发现里面大有文章.发出来研究研究: 最classical的答案:ID就像 ...

  7. 编程题:1. var person = '{name:"Lily",sex:"famale",age:24,country:"US"}';将person转换成JSON对象并便利每个属性值。

    /// <summary> /// Json工具类 /// </summary> public class JsonUtility { private static JsonU ...

  8. HDU 5056 Boring count(不超过k个字符的子串个数)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. SQL Server 2012 从备份中还原数据库

    1.首先把原数据库备份,检查原数据库的日志文件是否太大,如果过于大应该先收缩数据库日志 2.把备份的数据库文件在目标SQL Server还原,点击数据库,选择“还原文件或文件组” 3.如果需要修改还原 ...

  10. ip地址管理与子网划分

    1,高层协议(主机到主机或应用问题)负责名字到地址的映射.国际模块负责网际地址到局域网地址的映射.底层(如本地网或网关)程序的任务是负责本地网地址到路由上的映射. 2,地址是由4个八位字节组成(32位 ...