Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onActivityResult not triggering when Activity is started by LauncherActivity #458

Open
Thullner opened this issue Dec 9, 2023 · 0 comments

Comments

@Thullner
Copy link

Thullner commented Dec 9, 2023

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

  • Device: Samsung Galaxy S10e
  • OS: Android 11
  • Browsers Installed: Chrome
  • android-browser-helper library version: 2.5.0

Additional context

  • The problem occurs when MyAppLauncherActivity is started from the launcher.
  • If MyAppLauncherActivity is started from a non-launcher activity, it works as expected.
  • Manifest file includes necessary permissions and intent filters for MyAppLauncherActivity and CameraActivity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant