Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native android: How to save state on screen rotation or orientation

On rotating screen, it seems activity is getting restarted, it loses all it's state. I checked android developer guide.Handling Runtime Changes. It has mention about how to handle screen orientation and using onSaveInstanceState() before it destroys your activity and restore the state during onCreate() or onRestoreInstanceState().

like image 367
avatarfreak Avatar asked Nov 08 '15 07:11

avatarfreak


People also ask

How do I lock orientation in react native?

Screen orientation lock​ For Android, open the AndroidManifest. xml file and within the activity element add 'android:screenOrientation="portrait"' to lock to portrait or 'android:screenOrientation="landscape"' to lock to landscape.

How can I see my screen orientation in react native?

on iOS you need to allow your app to rotate. In XCode you need to go to target and then in the Deployment Info -> Device Orientation enable the orientaitons your app should support. For android you'll have to modify your AndroidManifest file and set the screenOrientation on the activity tag.

How do I change the orientation of android manifest?

The screen orientation attribute is provided by the activity element in the Android Manifest. Xml file. The orientations provided by the activity are Portrait, Landscape, Sensor, Unspecified and so on. To perform a screen orientation activity you define the properties in the Android Manifest.


2 Answers

I am answering to my question this is how it worked for me, if anyone is facing same problem, then edit your AndroidManifest.xml on android/app/src/main and local-cli/generator-android/templates/src/app/src/main

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
like image 188
avatarfreak Avatar answered Oct 06 '22 00:10

avatarfreak


Here is a detailed explanation of how to save/retrieve activity instance in Android

https://web.archive.org/web/20160308105435/http://www.intertech.com/blog/saving-and-retrieving-android-instance-state-part-1/

like image 45
Nika Kurdadze Avatar answered Oct 06 '22 00:10

Nika Kurdadze