티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Android - Preventing WebView reload on Rotate
Android - WebView에서 화면 회전 시 새로 고침 방지
문제 내용
When I rotate my screen, the WebView reloads the whole page. I can't have this since some of my content contains dynamic/random material. Currently when rotated the screen reloads the original URL from the loadUrl() method.
화면을 회전하면 WebView가 전체 페이지를 다시 로드합니다. 동적 / 무작위 콘텐츠가 포함 된 내용이 있기 때문에 이런 일이 일어나면 안됩니다. 현재, 화면이 회전되면 loadUrl() 메서드에서 원래 URL을 다시로드합니다.
Any idea what's wrong with my code?
제 코드에 문제가 뭔지 아시나요?
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>
높은 점수를 받은 Solution
I think the main problem is that you call web.loadUrl(webURL); also when savedInstanceState != null
저장된 인스턴스가 null이 아닌 경우 web.loadUrl (webURL); 를 호출하기 때문입니다.
EDIT
편집
Try:
다음을 시도하십시오.
if (savedInstanceState == null)
{
web.loadUrl(webURL);
}
EDIT2: You also need the onSaveInstanceState and onRestoreInstanceState override.
편집2 : 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);
}
Note: Please also add in your AndroidManifest.xml in your Activity android:configChanges="orientation|screenSize" Thanks
참고 : Activity android : configChanges = "orientation | screenSize"도 AndroidManifest.xml에 추가하십시오.
감사합니다.
가장 최근 달린 Solution
This solution works well for me:
이 해결책은 제게 잘 작동합니다
(1) In AndroidManifest.xml add this line android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
(1) AndroidManifest.xml에서이 행을 추가하십시오.
android : configChanges = "keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize"
Like this (and like the answers above)
이와 같이 (위의 대답과 같이)
<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) Then in MainActivity.java verify savedInstanceState
(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) and then:
(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);
}
출처 : https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate
'개발 > 안드로이드' 카테고리의 다른 글
안드로이드 WebView에서 JavaScript가 작동하지 않을 때 해결 방법 (0) | 2023.02.16 |
---|---|
Android MVVM ViewModel에서 Context 얻기 (0) | 2023.02.15 |
WebView 스레드 오류 수정하기 (0) | 2023.02.13 |
'This Handler class should be static or leaks might occur: IncomingHandler' 오류 수정하기 (0) | 2023.02.13 |
cordova build android" 명령어 실행 중 "unable to find attribute android:fontVariationSettings and android:ttcIndex" 오류 수정하기 (0) | 2023.02.12 |