Android TextView内容在屏幕锁定时丢失
我有一个里面有用户名的TextView,但当设备屏幕锁定(打开、关闭)时,里面的文本将消失。我尝试将Android TextView内容在屏幕锁定时丢失,android,textview,persist,Android,Textview,Persist,我有一个里面有用户名的TextView,但当设备屏幕锁定(打开、关闭)时,里面的文本将消失。我尝试将android:configChanges=“keyboardHidden | orientation”添加到ManifestFile中,并对TextView使用android:freezesText=“true”,但都没有成功 我还使用了: @Override protected void onSaveInstanceState(Bundle outState) { // TODO Au
android:configChanges=“keyboardHidden | orientation”
添加到ManifestFile中,并对TextView使用android:freezesText=“true”
,但都没有成功
我还使用了:
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("userName", username);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
String userName = savedInstanceState.getString("userName");
TextView viewName = (TextView)this.findViewById(R.id.username);
viewName.setText(userName);
}
但是onRestoreInstanceState不会在设备屏幕锁定时启动!
有人知道为什么会发生这种情况吗?我如何防止这种行为?
<TextView
...
android:freezesText="true" />
//or n order to retain data on orientation change you need to implement the two methods:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Read values from the "savedInstanceState"-object and put them in your textview
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// Save the values you need from your textview into "outState"-object
super.onSaveInstanceState(outState);
}
//或n为了保留有关方向更改的数据,您需要实施两种方法:
@凌驾
RestoreInstanceState上的受保护无效(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
//从“savedInstanceState”对象中读取值,并将其放入textview中
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
//将所需的值从textview保存到“outState”-对象中
super.onSaveInstanceState(超出状态);
}