Repository: tauri
Issue: [bug] Some runtime events are not delivered to the Tauri application (Android)
Labels: type: bug,status: needs triage,platform: Android

### Describe the bug

In Android platforms, some runtime events are not delivered to the Tauri application.

The Tauri application seems to catch `Ready` and `MainEventsCleared` events. However, events related to application termination (`ExitRequested`, `Exit`) and window events is not.

### Reproduction

1. Clone the Tauri repository and switch to `next` branch
2. Move to `examples/api` directory
3. (For debugging): modify codes to print events
    * Tauri application
        ```
        // src-tauri/src/lib.rs

        app.run(move |_app_handle, event| {
            println!("tauri-app-main: app event: {:?}", event);
        })
        ```
    * Android project
      ```
      // src-tauri/gen/android/api/app/src/main/java/com/tauri/api/MainActivity.kt

      import app.tauri.plugin.PluginManager
      import android.util.Log
      import android.os.Bundle

      class MainActivity : TauriActivity() {
        var pluginManager: PluginManager = PluginManager(this)

        override fun onCreate(savedInstanceState: Bundle?) {
            Log.i("invoked method", "onCreate@MainActivity(com.tauri.api)")
            super.onCreate(savedInstanceState)
        }

        override fun onStart() {
            Log.i("invoked method", "onStart@MainActivity(com.tauri.api)")
            super.onStart()
        }

        override fun onResume() {
            Log.i("invoked method", "onResume@MainActivity(com.tauri.api)")
            super.onResume()
        }

        override fun onPause() {
            Log.i("invoked method", "onPause@MainActivity(com.tauri.api)")
            super.onPause()
        }

        override fun onStop() {
            Log.i("invoked method", "onStop@MainActivity(com.tauri.api)")
            super.onStop()
        }

        override fun onWindowFocusChanged(hasFocus: Boolean) {
            Log.i("invoked method", "onWindowFocusChanged@MainActivity(com.tauri.api)")
            super.onWindowFocusChanged(hasFocus)
        }

        override fun onDestroy() {
            Log.i("invoked method", "onDestroy@MainActivity(com.tauri.api)")
            super.onDestroy()
        }
      }
      ```
4. Run Android application with `cargo tauri android dev`

### Expected behavior

Runtime events such as `ExitRequested`, `Exit` and `WindowEvent (e.g., Focused(false), Destroyed)` should be delivered to the Tauri application.

### Platform and versions

```text
[✔] Environment
    - OS: Windows 10.0.22621 X64
    ✔ WebView2: 111.0.1661.62
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.67.1 (d5a82bbd2 2023-02-07)
    ✔ Cargo: 1.67.1 (8ecd4f20a 2023-01-10)
    ✔ rustup: 1.25.2 (17db695f1 2023-02-01)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (environment override by RUSTUP_TOOLCHAIN)
    - node: 16.14.2
    - yarn: 1.22.19
    - npm: 8.5.0

[-] Packages
    - tauri [RUST]: 2.0.0-alpha.6
    - tauri-build [RUST]: 2.0.0-alpha.3
    - wry [RUST]: 0.27.1
    - tao [RUST]: 0.18.3
    - @tauri-apps/api [NPM]: 2.0.0-alpha.2
    - @tauri-apps/cli [NPM]: 2.0.0-alpha.6

[-] App
    - build-type: bundle
    - CSP: font-src https://fonts.gstatic.com; style-src 'unsafe-inline' 'self' https://fonts.googleapis.com; default-src 'self' customprotocol: asset:; img-src 'self' asset: https://asset.localhost blob: data:
    - distDir: ../dist
    - devPath: http://localhost:5173/
    - framework: Svelte
    - bundler: Vite
```


### Stack trace

```text
04-05 01:42:56.632   586  1995 I ActivityTaskManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.tauri.api/.MainActivity bnds=[228,1211][436,1526]} from uid 10135
04-05 01:42:56.726   586  2188 D CoreBackPreview: Window{9b82061 u0 Splash Screen com.tauri.api}: Setting back callback OnBackInvokedCallbackInfo{mCallback=android.window.IOnBackInvokedCallback$Stub$Proxy@33db847, mPriority=0}
04-05 01:42:56.734   586   614 I ActivityManager: Start proc 20116:com.tauri.api/u0a159 for next-top-activity {com.tauri.api/com.tauri.api.MainActivity}
04-05 01:42:56.756 20116 20116 I com.tauri.api: Late-enabling -Xcheck:jni
04-05 01:42:56.908 20116 20116 W com.tauri.api: Unexpected CPU variant for x86: x86_64.
04-05 01:42:56.908 20116 20116 W com.tauri.api: Known variants: atom, sandybridge, silvermont, kabylake, default
04-05 01:42:56.983   586  2191 I AppsFilter: interaction: PackageSetting{9e3693f com.tauri.api/10159} -> PackageSetting{f2760d2 com.google.android.apps.nexuslauncher/10135} BLOCKED
04-05 01:42:57.036 20116 20116 W ziparchive: Unable to open '/data/app/~~XzyVkx0oHmHowa7K4wvDGw==/com.tauri.api-mgkyFh6eIU4kLU1W23t1aQ==/base.dm': No such file or directory
04-05 01:42:57.036 20116 20116 W ziparchive: Unable to open '/data/app/~~XzyVkx0oHmHowa7K4wvDGw==/com.tauri.api-mgkyFh6eIU4kLU1W23t1aQ==/base.dm': No such file or directory
04-05 01:42:57.136 20116 20116 V GraphicsEnvironment: ANGLE Developer option for 'com.tauri.api' set to: 'default'
04-05 01:42:57.136 20116 20116 V GraphicsEnvironment: ANGLE GameManagerService for com.tauri.api: false
04-05 01:42:57.251 20116 20116 I invoked method: onCreate@MainActivity(com.tauri.api)
04-05 01:42:57.330 20116 20116 W com.tauri.api: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
04-05 01:42:57.332 20116 20116 W com.tauri.api: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
04-05 01:42:57.375 20116 20116 I invoked method: onStart@MainActivity(com.tauri.api)
04-05 01:42:57.376 20116 20116 I invoked method: onResume@MainActivity(com.tauri.api)
04-05 01:42:57.386   586  2188 D CoreBackPreview: Window{b6d1a u0 com.tauri.api/com.tauri.api.MainActivity}: Setting back callback OnBackInvokedCallbackInfo{mCallback=android.window.IOnBackInvokedCallback$Stub$Proxy@6a17a28, mPriority=0}
04-05 01:42:57.594 20116 20116 W com.tauri.api: Accessing hidden method Landroid/os/Trace;->isTagEnabled(J)Z (unsupported, reflection, allowed)
04-05 01:42:57.594 20116 20116 W com.tauri.api: Accessing hidden method Landroid/os/Trace;->traceBegin(JLjava/lang/String;)V (unsupported, reflection, allowed)
04-05 01:42:57.594 20116 20116 W com.tauri.api: Accessing hidden method Landroid/os/Trace;->traceEnd(J)V (unsupported, reflection, allowed)
04-05 01:42:57.594 20116 20116 W com.tauri.api: Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (unsupported, reflection, allowed)
04-05 01:42:57.594 20116 20116 W com.tauri.api: Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (unsupported, reflection, allowed)
04-05 01:42:57.691   586   614 I ActivityManager: Start proc 20157:com.google.android.webview:sandboxed_process0:org.chromium.content.app.SandboxedProcessService0:0/u0i16 for  {com.tauri.api/org.chromium.content.app.SandboxedProcessService0:0}
04-05 01:42:57.836 20116 20177 E chromium: [ERROR:simple_index_file_posix.cc(36)] opendir /data/user/0/com.tauri.api/cache/WebView/Default/HTTP Cache/Code Cache/js: No such file or directory (2)
04-05 01:42:57.836 20116 20145 I RustStdoutStderr: [ERROR:simple_index_file_posix.cc(36)] opendir /data/user/0/com.tauri.api/cache/WebView/Default/HTTP Cache/Code Cache/js: No such file or directory (2)
04-05 01:42:57.893 20116 20179 W com.tauri.api: Accessing hidden method Landroid/media/AudioManager;->getOutputLatency(I)I (unsupported, reflection, allowed)
04-05 01:42:57.895   586   602 W ziparchive: Unable to open '/data/app/~~XzyVkx0oHmHowa7K4wvDGw==/com.tauri.api-mgkyFh6eIU4kLU1W23t1aQ==/base.dm': No such file or directory
04-05 01:42:57.899   586   602 I ActivityTaskManager: Displayed com.tauri.api/.MainActivity: +1s261ms
04-05 01:42:57.949 20116 20145 I RustStdoutStderr: tauri-app-main: app event: Ready
04-05 01:42:57.949 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:42:57.949 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:42:57.949 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:42:57.999 20116 20116 I invoked method: onWindowFocusChanged@MainActivity(com.tauri.api)
04-05 01:42:58.006 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:42:58.024  6601  6601 I GoogleInputMethodService: GoogleInputMethodService.onStartInput():1903 onStartInput(EditorInfo{inputType=0x0(NULL) imeOptions=0x0 privateImeOptions=null actionName=UNSPECIFIED actionLabel=null actionId=0 initialSelStart=-1 initialSelEnd=-1 initialCapsMode=0x0 hintText=null label=null packageName=com.tauri.api fieldId=-1 fieldName=null extras=null}, false)
04-05 01:42:58.261   586  2188 D CoreBackPreview: Window{9b82061 u0 Splash Screen com.tauri.api EXITING}: Setting back callback null
04-05 01:42:58.263   586   918 W InputManager-JNI: Input channel object '9b82061 Splash Screen com.tauri.api (client)' was disposed without first being removed with the input manager!
04-05 01:43:01.886   586  2250 I InputDispatcher: Channel [Gesture Monitor] swipe-up (server) is stealing touch from [b6d1a com.tauri.api/com.tauri.api.MainActivity (server), [Gesture Monitor] edge-swipe (server)]
04-05 01:43:01.926 20116 20116 I invoked method: onWindowFocusChanged@MainActivity(com.tauri.api)
04-05 01:43:01.927 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:43:02.802 20116 20116 I invoked method: onPause@MainActivity(com.tauri.api)
04-05 01:43:02.803 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:43:02.829 20116 20116 I invoked method: onStop@MainActivity(com.tauri.api)
04-05 01:43:02.831 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:43:02.834 20116 20145 I RustStdoutStderr: tauri-app-main: app event: MainEventsCleared
04-05 01:43:05.017 20116 20116 I invoked method: onDestroy@MainActivity(com.tauri.api)
04-05 01:43:05.019   586   605 I ActivityManager: Killing 20116:com.tauri.api/u0a159 (adj 925): remove task
04-05 01:43:05.024   586  1995 D CoreBackPreview: Window{b6d1a u0 com.tauri.api/com.tauri.api.MainActivity}: Setting back callback null
04-05 01:43:05.036   586  1995 W ActivityManager: Scheduling restart of crashed service com.tauri.api/org.chromium.content.app.SandboxedProcessService0:0 in 1000ms for connection
04-05 01:43:05.054   586   918 I WindowManager: WIN DEATH: Window{b6d1a u0 com.tauri.api/com.tauri.api.MainActivity}
04-05 01:43:05.055   586   918 W InputManager-JNI: Input channel object 'b6d1a com.tauri.api/com.tauri.api.MainActivity (client)' was disposed without first being removed with the input manager!
04-05 01:43:05.072   586   612 W UsageStatsService: Unexpected activity event reported! (com.tauri.api/com.tauri.api.MainActivity event : 23 instanceId : 198416860)
```


### Additional context

Logs in desktop (Windows) platform.
```
[2023-04-04][15:18:42][api][INFO] got response: Ok(PingResponse { value: Some("test") })
tauri-app-main: app event: Ready
tauri-app-main: app event: MainEventsCleared
tauri-app-main: app event: WindowEvent { label: "main", event: Focused(true) }
tauri-app-main: app event: MainEventsCleared
tauri-app-main: app event: WindowEvent { label: "main", event: Focused(false) }
tauri-app-main: app event: MainEventsCleared
[2023-04-04][15:18:57][tao::platform_impl::platform::event_loop::runner][WARN] NewEvents emitted without explicit RedrawEventsCleared
[2023-04-04][15:18:57][tao::platform_impl::platform::event_loop::runner][WARN] RedrawEventsCleared emitted without explicit MainEventsCleared
tauri-app-main: app event: MainEventsCleared
tauri-app-main: app event: WindowEvent { label: "main", event: Focused(true) }
tauri-app-main: app event: MainEventsCleared
tauri-app-main: app event: WindowEvent { label: "main", event: Destroyed }
tauri-app-main: app event: ExitRequested { api: ExitRequestApi(Sender { .. }) }
tauri-app-main: app event: MainEventsCleared
tauri-app-main: app event: Exit
```