Skip to content

Fix the launch crash on Android under NativePHP v4 - #6

Open
Ikromjon1998 wants to merge 1 commit into
mainfrom
fix/android-v4-launch-crash
Open

Ikromjon1998 wants to merge 1 commit into
mainfrom
fix/android-v4-launch-crash

Conversation

@Ikromjon1998

Copy link
Copy Markdown
Owner

What

Fixes a crash at app launch on Android under NativePHP for Mobile v4. It affects 1.5.0-beta.1, the first version that can be installed on v4.

java.lang.IllegalStateException: LifecycleOwner com.nativephp.mobile.ui.MainActivity is attempting to
register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.
    at androidx.activity.ComponentActivity.registerForActivityResult
    at com.nativephp.documentscanner.DocumentScannerFunctions.registerLauncher
    at com.nativephp.documentscanner.DocumentScannerFunctions$Scan.<init>
    at ...registerPluginBridgeFunctions
    at com.nativephp.mobile.ui.MainActivity.initializeEnvironmentAsync

Why

NativePHP v3 calls registerBridgeFunctions() in MainActivity.onCreate. v4 moved it into initializeEnvironmentAsync, on a background thread after the activity has resumed. The Scan bridge function registered its result launcher with registerForActivityResult() in its constructor, and Android only allows that call before the activity is started.

The launcher is now registered with the activity's ActivityResultRegistry directly, which is valid at any point in the lifecycle. It is also re-registered when the activity instance changes.

Verified

On an Android 16 (API 36) emulator, in a Laravel 13 app with this plugin:

nativephp/mobile 4.6.0 3.3.9
App launches, before this change crashes with the trace above not run
App launches, with this change yes yes
"Scan" opens the Google scanner, with this change yes not run

The emulator image cannot download the ML Kit scanner module, so the scan itself stops at Google's download screen. A complete scan still needs a physical device.

composer check (133 tests, Pint, Rector, PHPStan) and ktlint pass.

Release

The changelog entry is 1.5.0-beta.2, replacing beta.1.

NativePHP v4 creates bridge functions on a background thread after the activity has resumed. The Scan function registered its result launcher with registerForActivityResult() in its constructor, which Android only allows before the activity is STARTED, so every v4 Android app with this plugin died at launch with an IllegalStateException. v3 creates bridge functions in onCreate and was not affected.

Register with the activity's result registry directly, which is valid at any point in the lifecycle, and re-register when the activity instance changes.

Reproduced and verified on an Android 16 emulator: the app crashed at launch on nativephp/mobile 4.6.0 before, and now launches on 4.6.0 and 3.3.9 and opens the scanner.
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

Successfully merging this pull request may close these issues.

1 participant