Android: persist instance state across multiple sessions
16lz
2021-01-23
Android: persist instance state across multiple sessions
freyo- JOINED:
- 07/27/2010
- POSTS:
- 122
July 21, 2011 15:53:47 Last update:July 21, 2011 15:53:47Use
SharedPreferences
to 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
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer