0

So I'm coding an Android app with Visual Studio. I want a certain Activity with both orientation portrait and landscape. When I turned my device, the data in my Activity disappeared. So I checked on internet and found a line to put in the manifest which keep the data and this worked ! android:configChanges="orientation|keyboardHidden|screenSize" except that all of my views were in disorder and deplaced.I'm using two different layout. one for the protrait view and the other for the landscape. I want to keep the data end switch the layout, any ideas ? thanx

Nino
  • 13
  • 4
  • 3
    learn about Activity Lifecycle. For your needs, onSaveInstantState is important....https://developer.android.com/guide/components/activities/activity-lifecycle.html – Opiatefuchs May 09 '17 at 11:44
  • Don't use `configChanges` it's a very bad way to handle orientation change. Basically you have to use an id on every so its state can be saved automaically. If you have data in variables, you can save them in `onSaveInstanteState` put putting them to the bundle and recover them in `onCreate` when Activity is restarted. Fragments can keep state with `setRetainInstante(true)` You can read more about that: [Android Developpers](https://developer.android.com/guide/topics/resources/runtime-changes.html), [Codepath](https://github.com/codepath/android_guides/wiki/Handling-Configuration-Changes) – Nicolas May 09 '17 at 11:47
  • 1
    Woah!! I got down voted so fast :O. Ok use this library it's easy https://github.com/frankiesardo/icepick – Samar Ali May 09 '17 at 11:52
  • Possible duplicate of [Android: alternate layout xml for landscape mode](http://stackoverflow.com/questions/4858026/android-alternate-layout-xml-for-landscape-mode) – Bharatesh May 09 '17 at 12:01
  • 1
    Possible duplicate of [Save state of activity when orientation changes android](http://stackoverflow.com/questions/13022677/save-state-of-activity-when-orientation-changes-android) – Sufian May 09 '17 at 15:16

2 Answers2

1

override

protected void onSaveInstanceState(Bundle outState) 

method and save value in outState variable in key value pair

in

protected void onCreate(Bundle savedInstanceState)

when orientation will chage savedInstanceState variable will have same value which you save in

onSaveInstanceState

Note: when activity will launch first time

protected void onCreate(Bundle savedInstanceState)

savedInstanceState will have null value

Shubham
  • 521
  • 4
  • 11
-2

Just put this line with activity tag of your Mainfest.xml file

android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"

Final code will be look like:

<activity android:name="com.xyz.myapp.MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"/>

Ashiq
  • 140
  • 4
  • It didn't work. It keep the data but I also want to change the view – Nino May 09 '17 at 11:52
  • write xml in each folder with same name `layout/main.xml` for portrait and `layout-land/main.xml` for landscape for more [info visit this](https://developer.android.com/training/basics/supporting-devices/screens.html) – Bharatesh May 09 '17 at 11:57
  • I did it but it won't change. I think the line i put in the manifest juste keep the data and don't handle the switch of layout – Nino May 09 '17 at 12:01