1

I am new to android. I am doing a sample project on calculation of numbers. And i am using both portrait and landscape layouts. Whenever I change the orientation, my activity is newly created and all the previously entered values get lost. So, I tried to stop activity recreation by android:configChanges="keyboardHidden|screenSize|orientation" and by setting orientation only to android:screenOrientation="portrait". But this makes my portrait layout to fit in landscape mode. But I don't want this. I need to load my separate portrait and landscape layouts on orientation change with same set of values which was previously created(i.e load different layouts without activity recreation).

And also I tried of using overridden methods like onConfigurationchanged, onSaveInstanceState, onRestoreInstanceState. But still values are lost in using onConfigurationchanged method , and activity is recreated in using onSaveInstanceState and onRestoreInstanceState methods (Still the previous values are retrieved here). Any solution for this? Thanks in advance.

Surendar
  • 79
  • 1
  • 1
  • 7
  • You should take a look at this answer: [1]: http://stackoverflow.com/questions/15858027/on-change-screen-orientation-new-activity-created – Álvaro Pérez Soria Dec 23 '14 at 07:05
  • 2
    google something called "onsaveinstancestate"..lol.. – Elltz Dec 23 '14 at 07:05
  • possible duplicate of [prevent activity restarting when orientation changes](http://stackoverflow.com/questions/17945130/prevent-activity-restarting-when-orientation-changes) – duggu Dec 23 '14 at 07:06

3 Answers3

3

To resolve this you need to save and retrieve data in/from saved instance state

@Override
 protected void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  super.onSaveInstanceState(outState);

  // Example to save the value of editText01 in SavedInstanceState
  String stateToSave = editText01.getText().toString();
  outState.putString("saved_state", stateToSave);

 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onRestoreInstanceState(savedInstanceState);

  //To Retrieve data from "saved_state"
  String stateSaved = savedInstanceState.getString("saved_state");
  edittextEditState.setText(stateSaved);
  }

 }

 }
Amit Thaper
  • 2,117
  • 4
  • 26
  • 49
  • I already tried this. This is just for saving and retrieving some values. But i need the whole activity unchanged on orientation change. – Surendar Dec 24 '14 at 05:34
  • For this you can set the this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in your onCreateMethod before setContentView And You can set the android:screenOrientation="portrait" in activity tag in AndroidManifest File – Amit Thaper Dec 24 '14 at 05:56
  • By setting that my portrait layout is tilted and fixed in the landscape view. But i need my landscape layout to be loaded which i have separately created. – Surendar Dec 24 '14 at 11:12
  • You need to create layout in layout and layout-land folder. In layout put the portrait_layout and in layout-land put the landscape layout. Layout name should be same in both folder. Use support screen tag in the android manifest file also. – Amit Thaper Dec 24 '14 at 11:17
  • I did this too. Now my problem is when i get appropriate portrait and landscape views fixed correctly in my device on orientation change, the activity is recreated. Meanwhile, when I stop the activity recreation, the same portrait layout get fixed in landscape views too. I just want to get both of these problems solved. – Surendar Dec 24 '14 at 12:55
  • There is no way in android to stop activity creation while change orientation. Activity always be recreated when we change device orientation. If you want to view change in landscape and portrait mode both then you need to remove orientation change code from AndroidManifest and setRequestedOrientation method from java code. – Amit Thaper Dec 24 '14 at 13:16
0

you should look at the onPause{} and onResume{} methods of the activity. because whenever your orientation is changed the activity is paused and then recreated again so you data is lost. and to understand better you should take a look at the activity life cycle http://developer.android.com/reference/android/app/Activity.html... :)

Umair
  • 6,366
  • 15
  • 42
  • 50
0

delete android:screenOrientation="portrait" and give this code, android:configChanges="keyboardHidden|screenSize"

Suren
  • 24
  • 7
  • Ya. Its working fine. But it is taking same layout for both portrait and landscape. But i need it to take separate layouts which i have created. – Surendar Dec 23 '14 at 07:34
  • use this code for separate layout in your activity, Configuration config = getResources().getConfiguration(); if (config.orientation == Configuration.ORIENTATION_PORTRAIT) { setContentView(R.layout.p_login); } else if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { setContentView(R.layout.l_login); } – Suren Dec 23 '14 at 07:39
  • I have same names for both portrait and landscape layouts. Even though I set it, the activity gets recreated. – Surendar Dec 24 '14 at 05:32