I created an app in Android Studio (2022.3.1) that just contains a webview and handles urls.
It worked until I added deep links via intent. Can someone tell me, whats wrong with that?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FullscreenDark"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.FullscreenDark">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="@string/app_host" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt:
package com.app.myapp
import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val host = getString(R.string.app_host)
webView = findViewById(R.id.webview)
val webSettings: WebSettings = webView.settings
// Configure settings
webSettings.javaScriptEnabled = true // Enable JavaScript
webView.webViewClient = MyWebViewClient()
// webView.webViewClient = WebViewClient()
// Check for deep link
val intentData = intent.data
if (intentData != null) {
val deepLink = intentData.toString()
webView.loadUrl(deepLink)
} else {
// Load your default URL
webView.loadUrl("https://$host")
}
}
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
super.onBackPressedDispatcher.onBackPressed()
}
}
private inner class MyWebViewClient : WebViewClient() {
@Deprecated("Deprecated in Java")
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
val host = getString(R.string.app_host)
// Load URLs from the same domain within the WebView
if (url != null && url.startsWith("https://$host")) {
view?.loadUrl(url)
return true
}
// Load all other URLs in the default browser
return false
}
}
}
I tried setting it up again from scratch several times and looked up other examples.
When I run it, I get this error everytime:
Unable to determine activity name
Update:
Changed AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FullscreenDark"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.FullscreenDark">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="@string/app_host" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
</application>
</manifest>
New error message: Activity class {com.app.myapp/com.app.myapp.MainActivity} does not exist
2
Answers
I found the solution to the last error:
I had to divide the intent-filters like this:
You are missing these tow lines under
<intent-filter>
:These tow lines indicates that this activity is the entry point for your application, or the Activity that starts first when your app launches.