본문 바로가기

프로젝트-주식

android- webView 회전 문제

핸드폰 자동회전을 꺼놔도 webView가 자동으로 회전을한다..

 

manifestes에 activity안에

android:screenOrientation="nosensor"
android:configChanges="orientation|screenSize|keyboardHidden"

 

activity에서 오버라이드

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}

 

webView 설정

val webView=binding.webView
val webSettings = webView.settings
webSettings.javaScriptEnabled = true

webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
val jsCode = "window.addEventListener('orientationchange', function() { if (window.orientation != 0) { screen.orientation.lock('portrait'); } });"
webView.evaluateJavascript(jsCode, null)
}

}

 

 

이렇게 해도 회전을 하지만... 한번 회전을 하면 계속 회전을 하진 않는다.

어떻게 하면 완전히 회전을 막을 수 있을까..?