handling state between landscape and portrait in android
I have a problem while developing application using layout and layout-land, the data is not maintain between those two layout. please help me. i also tried onConfigured()开发者_JS百科.
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
When the device orientation changed, the onCreate event is called again. You need to save any information that you have into a bundle on the onSaveInstanceState and reload it again on the onCreate event.
Code for OnCreate
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(savedInstanceState!= null)
{
restoreDataFromBundle(savedInstanceState);
}
}
Code for onSaveInstanceState
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
outState.putString("key", "value");
super.onSaveInstanceState(outState);
}
Code for restoreDataFromBundle
private void restoreDataFromBundle(Bundle savedInstanceState) {
String myString = savedInstanceState.getStringArray("key");
}
You have to declare this also in your manifest file if you would like to handle the screen changes yourself. Add this to your activity in AndroidManifest.xml
android:configChanges="keyboardHidden|orientation"
精彩评论