GoogleChrome / android-browser-helper

The Android Browser Helper library helps developers use Custom Tabs and Trusted Web Activities on top of the AndroidX browser support library.
Apache License 2.0
688 stars 286 forks source link

Not able to consistently launch another activity from the LauncherActivity #457

Closed Thullner closed 9 months ago

Thullner commented 9 months ago

Describe the bug Whenever I start another activity from my MyAppLauncherActivity it doesn't seem to start.

I can however get the other activity to start if I use a debugger, and do a "stepover" over the startActivity(). Another option is to delay the start of the activity. However this works very randomly (like 50% of the time). This is independent of the contents of the cameraActivity. Even if it just contains Logs it has the same issue.

Code

package com.my-app.android

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import com.google.androidbrowserhelper.trusted.LauncherActivity

class MyAppLauncherActivity : LauncherActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        intent?.data?.let { uri ->
            if (uri.scheme == "my-app" && uri.host == "capture-invoice") {
                val cameraIntent = Intent(this, CameraActivity::class.java).apply {
                    data = uri
                }

                Handler(Looper.getMainLooper()).postDelayed({
                    startActivity(cameraIntent)
                }, 100)
            }
        }
    }

    override fun getLaunchingUrl(): Uri {
        return Uri.parse("https://web.acc.my-app.dev")
    }
}
<?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-feature android:name="android.hardware.camera" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <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.MyAppAndroid"
        tools:targetApi="34">
        <activity
            android:name=".MyAppLauncherActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <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:host="web.acc.my-app.dev"
                    android:scheme="https" />
            </intent-filter>

            <intent-filter android:autoVerify="false">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="capture-invoice"
                    android:scheme="my-app" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CameraActivity"
            android:exported="false"></activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.my-app.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>

</manifest>

Expected behavior Expected is to start the other activity without needing a delay, and it starting every time.

Smartphone