android 手機螢幕翻轉 Activity 介面不更新

雖然用WebView可以顯示網頁,但是當手機翻轉後,他會重新再載入一次網頁,每翻轉一次就重新loading,使用者不舒服,service也會多負擔。

在 AndroidManifest.xml 加入

<manifest>
    <application>
        <activity 
            ...
            android:name=".MainActivity"
            ...
            android:configchanges="orientation|keyboardhidden|screensize">
              ↑ 加入這行 ↑
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
        </activity >
        ...
    </application>
</manifest>
從 Android 3.2 (API 13) 開始,螢幕翻轉螢幕尺寸也會跟著改變,所以要再加上 screenSize

在 Activity 中加入

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 什麼都不用寫
    } else {
        // 什麼都不用寫
    }
}



參考自:
官方:https://developer.android.com/guide/topics/resources/runtime-changes.html?hl=zh-tw
網誌:http://blog.csdn.net/chenzheng_java/article/details/6252162

留言

這個網誌中的熱門文章

C# 模擬鍵盤滑鼠控制電腦

android 定時通知(永久長期的) 本篇只講AlarmManager使用

python nn 聲音辨識 -1 傅立葉轉換

python pyautogui 簡介

python opencv 基本讀取、轉換、顯示、儲存等