Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – Opening the application from the results google.com, always directs to the home page of WebView Android

Posted on November 11, 2022 By

Solution 1 :

The solution is you need to handle app link first, get the correct link and then load that link into your WeView:

Uri data = getIntent().getData();

if (data != null) {
    String url = data.toString();
    webView.loadUrl(url);
} else {
    webView.loadUrl("https://siteadress.pl/");
}

Solution 2 :

Done! This version in “Kotlin” work:

val url = intent.data.toString()

if (URLUtil.isValidUrl(url)) {
    webView.loadUrl(url)
} else {
    webView.loadUrl("https://siteadress.pl/")
}

Thanks for help 🙂 ufff

Problem :

I apologize for my English.

I have a problem with redirecting to a subpage of Google results, SMS to WebView Android applications.

Example: The page with the address https://siteadress.pl/category in the Google results opens the WebView application and shows the homepage (https://siteadress.pl/)
LOOK PICTURE

Example: A page with the exact product https://siteadress.pl/shop/productxyz in Google results also opens the WebView application and shows the homepage. Why?

The link (https://siteadress.pl/shop/productxyz) from the sms message also opens the WebView application and shows the main page.

I want to point to the exact page of the application in WebView, not the homepage. 🙁

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/apk/res/android"
        package="pl.APP">

    <uses-permission android_name="android.permission.INTERNET" />

    <application
            android_allowBackup="true"
            android_icon="@mipmap/ic_launcher"
            android_label="APP"
            android_roundIcon="@mipmap/ic_launcher_round"
            android_supportsRtl="true"
            android_theme="@style/AppTheme">

        <meta-data
                android_name="asset_statements"
                android_resource="@string/asset_statements" />

        <activity
                android_name=".SplashScreen"
                android_launchMode="singleTop"
                android_noHistory="true"
                android_theme="@style/SplashTheme">
            <intent-filter>
                <action android_name="android.intent.action.MAIN" />

                <category android_name="android.intent.category.LAUNCHER" />
            </intent-filter>


            <meta-data
                    android_name="android.app.shortcuts"
                    android_resource="@xml/shortcuts" />
        </activity>


        <activity android_name=".ContactActivity" />
        <activity android_name=".CategoryActivity" />



        <activity
                android_name=".MainActivity"
                android_launchMode="singleTop"
                android_screenOrientation="portrait">
            <intent-filter android_autoVerify="true">
                <action android_name="android.intent.action.VIEW" />

                <category android_name="android.intent.category.DEFAULT" />
                <category android_name="android.intent.category.BROWSABLE" />

                <data
                        android_scheme="https"
                        android_host="siteadress.pl" />
            </intent-filter>
        </activity>

    </application>

</manifest>

My MainActivity.xml

package pl.APP

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.webkit.URLUtil
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*



class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        webView.webViewClient = MyWebViewClient()
        webView.loadUrl("https://siteadress.pl/")
        webView.settings.javaScriptEnabled = true
    }

    override fun onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack()
        } else {
            super.onBackPressed()
        }
    }

    inner class MyWebViewClient : WebViewClient()
    {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean
        {
            if(URLUtil.isNetworkUrl(url))
            {
                return false
            }
            try
            {
                val shareIntent= Intent()
                shareIntent.action=Intent.ACTION_VIEW
                shareIntent.data= Uri.parse(url)
                startActivity(shareIntent)
            }
            catch(e: ActivityNotFoundException)
            {
                Toast.makeText([email protected], "Appropriate app not found", Toast.LENGTH_LONG).show()
                Log.e("AndroidRide",e.toString())
            }
            return true
        }
        @RequiresApi(Build.VERSION_CODES.N)
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean
        {
            val url=request?.url.toString()
            if(URLUtil.isNetworkUrl(url))
            {
                return false
            }
            try
            {
                val shareIntent= Intent()
                shareIntent.action=Intent.ACTION_VIEW
                shareIntent.data= Uri.parse(url)
                startActivity(shareIntent)
            }
            catch(e: ActivityNotFoundException)
            {
                Toast.makeText([email protected], "Appropriate app not found", Toast.LENGTH_LONG).show()
                Log.e("AndroidRide",e.toString())
            }
            return true

        }

    }


}

Thanks for help 🙂

READ  [FIXED] android - Can the Media Codec decoders output RGB-like formats?
Powered by Inline Related Posts

Comments

Comment posted by Subaru Tashiro

Perhaps the title of your question should be in the form of a question and without “KOTLIN” in it. If you prefer answers with example code in Kotlin, you should ask that in the body of your question post instead of the title. If your question is “why does my webview only open

Comment posted by sadam1989

I’m trying. Link redirection now works. Turning on the application does not work now. I am a poor programmer. Will you help

Comment posted by Loremar Marabillas

@sadam1989 I updated the answer. It should work now.

Comment posted by sadam1989

receives information: Condition ‘url != null’ is always ‘true’

Comment posted by sadam1989

I have corrected your code. Make Project ok. Redirection works. I turn on the simulator, application (white page). I give up…

Comment posted by Loremar Marabillas

@sadam1989 I’m sorry I keep getting it wrong. I have updated my code. I am now 100% sure this works.

Android Tags:android, kotlin, webview

Post navigation

Previous Post: [FIXED] Xamarin.forms – Trouble behaviour between ListView & keyboard
Next Post: [FIXED] android – two elements in linear layout are packed to center instead of to the sides

Related Posts

[FIXED] android – How to get the percentage of crash free users programmatically from Firebase Android
[FIXED] c# – Xamrin.Forms trying to create Email check using Firebase Realtime Database Android
[FIXED] java – how to pass a decimal value using number type Edittext in Android Studio? Android
[FIXED] android – DropDownPreference setOnPreferenceChangeListener calls itself when initialized Android
[FIXED] android – How to inject viewmodel in a customView with koin? Android
[FIXED] android – java.lang.IllegalStateException: getResources() or getAssets() has already been called Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme