【问题标题】:Preventing pure HTML 5 mobile app from reseting when changing orientation in Android在 Android 中更改方向时防止纯 HTML5 移动应用程序重置
【发布时间】:2014-01-31 15:51:56
【问题描述】:

我正在使用 jquery mobile 开发一个纯 HTML 5 移动应用程序,除了加载我的主 HTML 文件之外,对 Android 代码的依赖非常少。

以下是我的 MainActivity 代码。

public class MainActivity extends Activity {

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


    webView.loadUrl("file:///android_asset/main.html");
    setContentView(webView);
}


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

以下是我的 Mainifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_playlist"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.android_playlist.MainActivity"
        android:label="@string/app_name" 
        android:configChanges="orientation|keyboard|keyboardHidden" 
        android:screenOrientation="sensor" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

根据 android 文档,当方向改变时,Android 会重新加载 Activity。 就我而言,我的单个活动是一个包含视频列表的 HTML5 文件。现在,当我在我的 Android 手机上对此进行测试时,页面会在我更改方向时自行重新加载。 我想避免这种情况。有人可以建议我解决这个问题。

注意:在我的活动中,我只是加载此页面,其余所有代码都在客户端,即使用 jquery mobile,所以我不确定如何告诉我的 Android 保存我的 HTML5 页面状态。

此外,有时我注意到,当应用程序崩溃或我多次点击返回按钮以退出 应用程序时,我的 localStorage 会变得清晰。

我正在使用以下语法将任何项目存储到 localStorage。

localStorage.setItem() and localStorage.getItem()

当我将 json obj 存储到 localStorage 中时,我还使用了以下 2 个实用程序函数。

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

【问题讨论】:

    标签: android html jquery-mobile local-storage


    【解决方案1】:

    将此用于主要活动

    <activity android:name=".SomeActivity"
              android:label="@string/app_name"
              android:screenOrientation="landscape">
    

    或者你可以使用

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    【讨论】:

    • 我今天会试试这个,同时你能不能看看我关于localStorage的另一个问题......
    • 将本地存储与窗口对象 window.localStorage.setItem() 绑定。你在用 phonegap 吗?
    • 不,我没有使用任何 phonegap 或任何其他框架
    【解决方案2】:

    如果您希望允许以任一方向查看您的应用,并且在方向更改时不重启应用,您只需在清单文件的 Activity 中添加 screenSize。

    <activity
        android:name="com.example.android_playlist.MainActivity"
        android:label="@string/app_name" 
        android:configChanges="orientation|keyboard|keyboardHidden|screenSize" 
        android:screenOrientation="sensor" >
    

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2011-04-11
      • 2016-07-14
      • 1970-01-01
      相关资源
      最近更新 更多