如何存储值并在方向改变时检索它?
我检查了很多帖子,并据此我已经完成了方向更改的编码。当方向改变时,问题是我无法检索 TextView 中输入的值。谁能告诉我哪里错了?
编码:
在我添加的相应活动的清单文件中:
android:configChanges="orientation|keyboardHidden"
在活动中,我添加了以下方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
//Initialized the widgets
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//have written separate layout files for portrait and landscape
setContentView(R.layout.home_screen);
//Initialized the widgets again
retrieveSavedState(); //sets the TextViews again
}
@Override
protected void onPause() {
super.onPause();
saveState(); //save the TextView values
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
}
@Override
protected void onResume() {
super.onResume();
retrieveSavedState();
}
I have checked many posts, and as per that I have done coding for the orientation change. When orientation changes, the problem is I am not able to retrieve the entered values inside TextViews. Can anyone plz tell where did I go wrong?
Coding:
In manifest file for the corresponding activity I added:
android:configChanges="orientation|keyboardHidden"
In activity, I added the following methods:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
//Initialized the widgets
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//have written separate layout files for portrait and landscape
setContentView(R.layout.home_screen);
//Initialized the widgets again
retrieveSavedState(); //sets the TextViews again
}
@Override
protected void onPause() {
super.onPause();
saveState(); //save the TextView values
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
}
@Override
protected void onResume() {
super.onResume();
retrieveSavedState();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的方法可用于保存任何对象来保存数据...
并且您可以在
onCreate()
中检索该对象,当方向改变时我们将调用该对象..从该对象读取数据后,您可以可以在任何你想要的地方使用它..http://developer.android.com/guide/topics/resources/runtime-changes.html
http://developer.android.com/guide/topics/resources/runtime-changes.html
其他机制也有,但这是最好的方法据我所知..您还可以使用共享首选项来存储和检索数据...
http ://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html
the above method can be used to save any object to save data...
and u can retreive that object in the
onCreate()
which will we called when orientation changes.. after reading data from that object you can use it where ever you want..http://developer.android.com/guide/topics/resources/runtime-changes.html
http://developer.android.com/guide/topics/resources/runtime-changes.html
other mechanisms are also there but this is the best way for my knowledge.. you can also use shared preferences to store and retrieve data...
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html