SURYA KARUTURI
SURYA KARUTURI

Reputation: 119

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().

public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
}

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);   
}

Upvotes: 0

Views: 2361

Answers (2)

Daniel Novak
Daniel Novak

Reputation: 2756

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"

Upvotes: 0

atbebtg
atbebtg

Reputation: 4083

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");
}

Upvotes: 2

Related Questions