当前位置: 移动技术网 > IT编程>移动开发>Android > Android保存Activity状态的方法

Android保存Activity状态的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

圣龙铁血,公益活动策划书范文,电动汽车安全性

本文实例讲述了android保存activity状态的方法。分享给大家供大家参考,具体如下:

如果你想保存activity的信息(例如,类实例的变量)而又不需要和其它的组件共享的话,你可以调用activity的getpreferences方法,不用指定一个preference的名字。对返回的shared  preference的访问只限于调用的activity;每个activity支持一个不命名的shared preference对象。

下面的框架代码显示了如何使用activity的私有shared preference:

protected void saveactivitypreferences()
{
// create or retrieve the activity preferences object.
sharedpreferences activitypreferences = getpreferences(activity.mode_private);
// retrieve an editor to modify the shared preferences.
sharedpreferences.editor editor = activitypreferences.edit();
// retrieve the view
textview mytextview = (textview)findviewbyid(r.id.mytextview);
// store new primitive types in the shared preferences object.
editor.putstring("currenttextvalue", mytextview.gettext().tostring());
// commit changes.
editor.commit();
}

保存和恢复实例状态

对于保存activity实例的变量来说,android提供了一种替代shared preference的特殊方法。

通过重写activity的onsaveinstancestate事件处理函数,你可以使用它的bundle参数来保存实例的值。保存数据的方法还是使用与在shared preference中相同的get和put方法。在完成bundle的修改后,将其传入父类的处理函数中,如下面的代码片段所示:

private static final string textview_state_key = "textview_state_key";
@override
public void onsaveinstancestate(bundle outstate) {
// retrieve the view
textview mytextview = (textview)findviewbyid(r.id.mytextview);
// save its state
outstate.putstring(textview_state_key,
mytextview.gettext().tostring());
super.onsaveinstancestate(outstate);
}

这个处理函数会在activity的active生命周期结束时触发,但仅在它不是显式地结束(即异常结束)。因此,它一般用于确保在单个用户会话中的active生命周期间activity状态的一致性。

如果一个会话期间,应用程序被迫重启,那么,保存的bundle会传入到onrestoreinstancestate和oncreate方法中。下面的片段显示了如何从bundle中提取值来更新activity实例的状态:

@override
public void oncreate(bundle icicle) {
super.oncreate(icicle);
setcontentview(r.layout.main);
textview mytextview = (textview)findviewbyid(r.id.mytextview);
string text = "";
if (icicle != null && icicle.containskey(textview_state_key))
text = icicle.getstring(textview_state_key);
mytextview.settext(text);
}

有一点很重要的是,记住onsaveinstancestate仅在activity变成非active状态时调用,但不在调用finish来关闭它或用户按下back按钮时调用。

保存to-do list activity的状态

目前,每一次to-do list例子程序重新启动时,所有的to-do项都丢失了且任何在文本输入框中输入的文字也被清除了。在这个例子中,你将在会话期间保存to-do list程序的状态。

todolist activity中的实例状态由三个变量组成:

① 是否一个新的item正在添加?

② 在新的项目输入框中存在什么样的文字?

③ 哪个是当前选择的项目?

使用activity默认的shared preference,你可以保存这些值,当activity重启时更新ui。

在本章的后面,你将学习如何使用sqlite去保存to-do项目。这个例子是第一步,演示如何通过保持activity实例的细节来确保无瑕疵的体验。

1. 添加静态的字符串用作preference的键。

private static final string text_entry_key = "text_entry_key";
private static final string adding_item_key = "adding_item_key";
private static final string selected_index_key = "selected_index_key";

2. 接下来,重写onpause方法。获得activity的私有shared preference并得到它的editor对象。

使用第1步中创建的键,存储实例的值,包括是否一个新的项目正在添加和任何在"new item"输入框中的文本。

@override
protected void onpause(){
super.onpause();
// get the activity preferences object.
sharedpreferences uistate = getpreferences(0);
// get the preferences editor.
sharedpreferences.editor editor = uistate.edit();
// add the ui state preference values.
editor.putstring(text_entry_key, myedittext.gettext().tostring());
editor.putboolean(adding_item_key, addingnew);
// commit the preferences.
editor.commit();
}

3. 编写一个restoreuistate方法,当程序重启时,应用在第2步中记录的实例的值。

修改oncreate方法,在最后部分添加对restoreuistate方法的调用。

@override
public void oncreate(bundle icicle)
{
[ ... existing oncreate logic ... ]
restoreuistate();
}
private void restoreuistate()
{
// get the activity preferences object.
sharedpreferences settings = getpreferences(activity.mode_private);
// read the ui state values, specifying default values.
string text = settings.getstring(text_entry_key, "");
boolean adding = settings.getboolean(adding_item_key, false);
// restore the ui to the previous state.
if (adding)
{
addnewitem();
myedittext.settext(text);
}
}

4. 使用onsaveinstancestate/onrestoreinstancestate机制来记录当前选择的项目的索引。它仅在非用户显式的指令杀死应用程序时保存和应用。

@override
public void onsaveinstancestate(bundle outstate)
{
outstate.putint(selected_index_key, mylistview.getselecteditemposition());
super.onsaveinstancestate(outstate);
}
@override
public void onrestoreinstancestate(bundle savedinstancestate)
{
int pos = -1;
if (savedinstancestate != null)
if (savedinstancestate.containskey(selected_index_key))
pos = savedinstancestate.getint(selected_index_key, -1);
mylistview.setselection(pos);
}

当你运行to-do list程序时,你应该看到了在会话期间ui状态的保存。但是,它还不能保存to-do列表的项目——你将在本章的后面添加这个核心的功能。

更多关于android相关内容感兴趣的读者可查看本站专题:《android编程之activity操作技巧总结》、《android视图view技巧总结》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网