スポンサーリンク

FragmentのwebViewでshouldOverrideUrlLoading【Kotlin】

エンジニア

FragmentのwebViewでshouldOverrideUrlLoadingに苦労したので、メモとして残します。

URL遷移時にこのメソッドに入ってきます。

今回はurlの内容によって画面の縦横を固定しました。

【Kotlin】

ModalFragment.kt

package com.example.

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment


class ModalFragment : Fragment() {
    lateinit var webView: WebView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        val v: View = inflater.inflate(R.layout.fragment_modal, container, false)
        activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        WebView.setWebContentsDebuggingEnabled(true)
        webView = v.findViewById<View>(R.id.web_view) as WebView
        true.also { webView.settings.javaScriptEnabled = it } //jsを有効に

        (activity as? MainActivity)?.let { activity ->
            webView.addJavascriptInterface(WebAppInterface(activity), "Android")
            webView.settings.javaScriptEnabled = true
            webView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                    if (url == NETWORK_ADDRESS + "report") {
                        //URLに"/logout"文字が含まれているとき
                        activity.requestedOrientation =
                            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE // 横画面固定
                        return true
                    } else {
                        activity.requestedOrientation =
                            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT // 縦画面固定
                    }
                    return true
                }
            }
        }
        webView.loadUrl("javascript:window.location.href='" + NETWORK_ADDRESS + "'")
        return v
    }

fragment_modal.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".ModalFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_modal_fragment" />

    <Button
        android:id="@+id/btn_load_js"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="400dp"
        android:layout_marginLeft="200dp"
        android:text="LOADJSButton" />

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">
    </WebView>

</FrameLayout>

 

詰まった点

webView.webViewClient =

の書き方

webView.loadUrl("javascript:window.location.href='" + NETWORK_ADDRESS + "'")

の書き方

コメント