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
694 stars 288 forks source link

onActivityResult not triggering when Activity is started by LauncherActivity #458

Open Thullner opened 10 months ago

Thullner commented 10 months ago

Describe the bug The MyAppLauncherActivity, which is started by the LauncherActivity, does not seem to return a result when it should. However, if I start an activity from a non-launcher activity, it works as expected. Additionally, when the intent for the CameraActivity is inside the main activity, the CameraActivity does not receive the ActivityResult from the camera.

To Reproduce Steps to reproduce the behavior:

  1. Launch the application.
  2. The MyAppLauncherActivity is started as the launcherActivity.
  3. In MyAppLauncherActivity, perform an action that should return a result (e.g., starting CameraActivity).
  4. Complete the action in CameraActivity.
  5. Expect MyAppLauncherActivity to receive the result, but it doesn't.

Expected behavior MyAppLauncherActivity should receive the result from CameraActivity when it is started from the launcherActivity. Additionally, CameraActivity should receive the ActivityResult when its intent is inside the main activity.

Did this ever used to work No, it has never worked as expected.

Screenshots N/A

Code Snippets MyAppLauncherActivity code:

package me.myapp.core.android

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

class MyAppLauncherActivity : LauncherActivity() {

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

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

                startActivityForResult(cameraIntent, CAMERA_ACTIVITY_REQUEST_CODE)
            }
        }
    }

    override fun getLaunchingUrl(): Uri {
        val uri = this.intent.data

        Log.d("MyAppLog", "Using URL from Intent ($uri).")

        if (uri != null) {
            return uri
        }

        return Uri.parse("https://web.acc.myapp.dev")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d("MyAppLog", "LauncherActivity - onActivityResult: $requestCode, $resultCode, $data")

        if (requestCode == CAMERA_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            val redirectIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.acc.myapp.dev/received-invoices"))
            startActivity(redirectIntent)
        }
    }

    companion object {
        private const val CAMERA_ACTIVITY_REQUEST_CODE = 1003
    }
}

The last piece of code run in the CameraActivy.kt (tested with logs and debugger)

            setResult(RESULT_OK) // Set the result before finishing
            Log.d("MyAppLog", "Image sent to server.")
            finish()

Manifest

<?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.INTERNET" />
    <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.myapp.dev"
                    android:scheme="https" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CameraActivity"
            android:exported="true">
            <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="capture-invoice"
                    android:scheme="myapp" />
            </intent-filter>
        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="me.myapp.core.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>

Smartphone

Additional context