保存和屏幕方向切换时恢复ButtonText [英] Save and restore a ButtonText when screen orientation is switched

查看:165
本文介绍了保存和屏幕方向切换时恢复ButtonText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在android系统往常一样,每次屏幕翻转人像/风景模式中通过活动的onSaveInstanceState从生命周期的onDestroy运行,然后重新创建。

As usual in android, each time the screen is flipped to portrait/landscape mode an Activity runs through life-cycle from onSaveInstanceState to onDestroy and then is recreated.

在我的活动有可以由用户改变一个ButtonText。它reseted到初始状态,但我不得不以某种方式保存最后的状态。我怎样才能做到这一点,我将不得不覆盖的onSaveInstanceState?可有人告诉一个例子吗?

In my Activity there's a ButtonText which can be changed by the user. It's reseted to the initial state, but I have to save the last state somehow. How can I achieve that, will I have to override onSaveInstanceState? Can someone show an example?

推荐答案

第1步:呼叫的getText()的toString()按钮来得到的标题。

Step #1: Call getText().toString() on the Button to get the caption.

第二步:拨打 putString()捆绑传递给你的执行的onSaveInstanceState()来存储的标题。

Step #2: Call putString() on the Bundle passed to your implementation of onSaveInstanceState() to store the caption.

第三步:呼叫的getString()捆绑传递给执行 onRestoreInstanceState()(或的onCreate(),如果捆绑不是,如果你想)让您的字幕回来,然后调用的setText()按钮把标题回去。

Step #3: Call getString() on the Bundle passed to your implementation of onRestoreInstanceState() (or in onCreate(), if the Bundle is not null, if you wish) to get your caption back, then call setText() on your Button to put the caption back in.

下面是一个使用示例项目 的onSaveInstanceState()保存乌里联系人的(这也影响不论是否按钮已启用)。

Here is a sample project that uses onSaveInstanceState() to save a Uri of a contact (which also affects whether or not a Button is enabled).

这篇关于保存和屏幕方向切换时恢复ButtonText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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