Android: persist instance state across multiple sessions

freyo
JOINED:
07/27/2010
POSTS:
122
(Not rated)
July 21, 2011 15:53:47 Last update:July 21, 2011 15:53:47UseSharedPreferencesto persist an instance state. The state is saved even when the app is killed.
public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);            powerStatus = prefs.getString("power", "On");    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);    int n = rg.getChildCount();    for (int i = 0; i < n; i++) {RadioButton rb = (RadioButton) rg.getChildAt(i);if (powerStatus.equals(rb.getText().toString())) {    rg.check(rb.getId());    break;}    }}      public void setValues(View view) {    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);    powerStatus = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();            SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);    // Use Editor to change values    SharedPreferences.Editor editor = prefs.edit();    editor.putString("power", powerStatus);     // do not forget to commit the changes!    editor.commit();}
Share|| Comment| Tags

Related Notes

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android中handler案例
  2. Android(安卓)应用程序开发基础
  3. android activity开发文档翻译 - 1 - 基
  4. 你还在问android横竖屏切换的生命周期?
  5. 招聘android架构师(北京)
  6. 竞争对手短期内不足以威胁Android地位
  7. Android(安卓)、BlackBerry 文本对齐方式
  8. Android(安卓)TextView 添加下划线的几种
  9. android 中 Message详解
  10. JAVA效率真的很低吗?Android为什么要采用