ProgramingTip

Android- 회전시 WebView 다시로드 방지

bestdevel 2020. 10. 20. 07:56
반응형

Android- 회전시 WebView 다시로드 방지


화면을 회전하면 WebView가 전체 페이지를 다시로드합니다. 내 콘텐츠 중 일부에 동적 / 무작위 자료가 포함되어 있기 때문에 발생합니다. 현재 화면을 회전하면 loadUrl () 메서드에서 원래 URL이 다시로드됩니다.

내 코드에 어떤 문제가 있는지 아십니까?

MainActivity.java

package com.mark.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    WebView web;
    String webURL = "http://www.google.co.uk/";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState != null)
            ((WebView)findViewById(R.id.web)).restoreState(savedInstanceState);

        web = (WebView) findViewById(R.id.web);
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl(webURL);
        web.setPadding(0, 0, 0, 0);
        web.getSettings().setLoadWithOverviewMode(true);
        web.getSettings().setUseWideViewPort(true);
        web.getSettings().setSupportZoom(true);
        web.getSettings().setBuiltInZoomControls(true);

        web.setWebViewClient(new HelloWebViewClient());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class HelloWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView web, String url) {
            web.loadUrl(url);
            return true;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mark.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"

            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

주된 문제는 web.loadUrl (webURL); 또한 savedInstanceState! = null 일 때

편집하다

시험 :

if (savedInstanceState == null)
{
  web.loadUrl(webURL);
}

EDIT2 : onSaveInstanceState 및 onRestoreInstanceState 재정의도 필요합니다.

@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}

참고 : 또한 활동에 AndroidManifest.xml을 추가하십시오 android : configChanges = "orientation | screenSize"감사합니다.


자바 코딩이 필요하지 않습니다. 매니페스트 파일에서 사용하십시오.

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

처럼 :

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.Example.WebviewSample.webviewsample"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>        
</application>

태그 (매니페스트)

android:configChanges="orientation|screenSize"

android:configChanges="orientation|screenSize"매니페스트 추가 는 나를 위해 작동합니다.

<activity
            android:name="com.example.HelloWorld.WebActivity"
            android:label="@string/title_activity_web"
            android:configChanges="orientation|screenSize" >
</activity>

나는 이것이 더 작동하지 않을 것이라고 믿습니다. 제 경우에는 WebView.restore(Bundle savedInstanceState)여전히 사용하여 상태를 복원 하면 URL이 다시로드됩니다.

문서보면 다음과 같이 restoreState()표시됩니다.

이 WebView가 상태 (페이지로드, 뒤로 / 앞으로 목록 만들기 등)를 빌드 할 수있는 기회가있는 후에 호출 할 수있는 기회가있을 수 있습니다.

이 방법은 더 이상이 WebView에 대한 앰 퍼런 스 데이터를 복원하지 않습니다.

그의 대답 에서 올바른 방향으로 나를 @ e4c5 소품

물론 극단적 인 조치는 방향 변경이 활동을 파괴 / 생성을 유발하는 것을 방지하는 것입니다. 이를 수행하는 방법에 대한 문서는 여기에 있습니다.


매니페스트 에이 코드 추가

<activity 
     ...
     android:configChanges="orientation|screenSize">

onConfigChange방향 변경시 데이터를 다시로드하지 않도록 조치 재정의

AndroidMainfest파일 의 활동에 .

android:configChanges="orientation|keyboardHidden" 

WebView 설정

webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webview.loadUrl("Your URL To Load");

매니페스트 파일에서 다음을 시도하십시오.

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

Manifest.xml 파일 활동에 다음을 배치하십시오.

android:configChanges="orientation|screenSize"

다음은 그 예입니다.

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar"
          android:configChanges="orientation|screenSize">
          <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
</activity>

여기인용 된 것과 같이 ,

주의 : Android 3.2 (API 레벨 13)부터는 기기가 세로 방향과 가로 방향 사이를 전환 할 때 '화면 크기'도 변경됩니다. 따라서 API 레벨 13 이상 (minSdkVersion 및 targetSdkVersion 속성에 의해 선언 됨) 용으로 개발할 때 방향 변경으로 인해 런타임이 다시 시작되지 않도록하려면 "orientation"값 외에 "screenSize"값을 포함해야합니다. 즉, android : configChanges = "orientation | screenSize"를 표시해야합니다. 그러나 애플리케이션이 API 레벨 12 이하를 대상으로하는 경우 활동은 항상이 구성 변경을 자체적으로 처리합니다 (이 구성 변경은 Android 3.2 이상 기기에서 실행되는 경우에도 활동을 다시 시작하지 않음).

android:configChanges="orientation|screenSize"활동을 설정 하면이 문제가 해결됩니다.

또한 다음 사항에 유의하십시오.

알아두기 : 구성 변경을 처리하기 위해 활동을 선언 할 때 대안을 제공하는 모든 요소를 ​​재설정해야합니다. 방향 변경을 처리하기 위해 활동을 선언하고 가로와 세로 사이에서 이미지가 변경되어야하는 경우 onConfigurationChanged () 중에 각 리소스를 각 요소에 다시 할당해야합니다.


이 솔루션은 저에게 잘 작동합니다.

(1) AndroidManifest.xml에서 android : configChanges = "keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize"

이렇게 (그리고 위의 답변처럼)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

(2) 그런 다음 MainActivity.java에서 savedInstanceState를 확인하십시오.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainContext = getApplicationContext();
    ----
    myWebView = (WebView) findViewById(R.id.webView);
    prepareWebView();
    myWebView.addJavascriptInterface(myJavaScriptInterface, "WEB2Android");

    if (savedInstanceState == null) {
        myWebView.post(new Runnable() {
            @Override
            public void run() {
                myWebView.loadUrl("http://www.appbiz.ro/foto_konta");
            }
        });
    }
    ----
}

(3) 다음 :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
protected void onSaveInstanceState(Bundle outState )
{
    super.onSaveInstanceState(outState);
    myWebView.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);
    myWebView.restoreState(savedInstanceState);
}

참고 URL : https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate

반응형