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 🙂
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.