Shantanoo K Shantanoo K - 1 year ago 47
Android Question

Preventing pure HTML 5 mobile app from reseting when changing orientation in Android

I am developing a pure HTML 5 mobile app using jquery mobile and very less dependency on Android code apart from loading my main HTML file.

Following is my MainActivity code.

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
// setContentView(R.layout.activity_main);
WebView webView = new WebView(this);
WebSettings settings = webView.getSettings();
// TO enable JS
// To enable Localstorage


public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(, menu);
return true;

Following is my Mainifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
android:targetSdkVersion="18" />
android:theme="@style/AppTheme" >
android:screenOrientation="sensor" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

As per the android documentation when the orientation is changed the Android re-loads the activity.
In my case my single activity is a single HTML5 file which contains list of videos. Now when I am testing this on my Android mobile the page reloads itself when I change my orientation.
I would like to avoid this. Can someone please suggest me a way to this.

Note: in my activity I am just loading this page and rest all code is at client side i.e. using jquery mobile so I am not sure how I will tell my Android to save my HTML5 page state.

Also, sometimes I noticed that when The app is crashes or if I hit the back button more than once to come out of the application my localStorage is getting clear.

I am using following syntax to store any item into localStorage.

localStorage.setItem() and localStorage.getItem()

Also I am using following 2 utility functions as I am storing json obj as well into localStorage.

Storage.prototype.setObj = function(key, obj) {
return this.setItem(key, JSON.stringify(obj));
Storage.prototype.getObj = function(key) {
if (this.getItem(key) != "undefined") {
return JSON.parse(this.getItem(key));
} else {
return null;

Answer Source

Use this for main activity

<activity android:name=".SomeActivity"

Or you can use

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);