android icon indicating copy to clipboard operation
android copied to clipboard

command_webview should not relaunch application

Open kr0ner opened this issue 6 months ago • 3 comments

Is your feature request related to a problem? Please describe. I'm trying to display the front door camera feed whenever a person is detected on the stairs. This works well, but switching the view via command_webview relaunches the already running companion app and introduces about 5 - 10 seconds latency. After some days the app seems to crash completely and needs to be manually restarted. That did not happen before.

Describe the solution you'd like In my context it makes no sense to restart the already running HA companion app ... I just want to switch tabs. Not sure if there is any use case at all that requires multiple instances. If not, this could fix my problem:

android:launchMode="singleInstance"

alternatively what this guy suggested on stack overflow

In general you can use a combination of Intent.FLAG_ACTIVITY_SINGLE_TOP, Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and Intent.FLAG_ACTIVITY_NEW_TASK to accomplish what you want. However, which one (or ones) to use depends on the situation you have

https://stackoverflow.com/a/11555704

Describe alternatives you've considered, if any I tried the solution above, but was not able to test it, because the push notifications require firebase configuration which I did not manage to get running.

Additional context

alias: Türkamera auf Tablet EG anzeigen wenn Person(en) erkannt
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.frontdoor_person_count
    above: 0
conditions: []
actions:
  - action: notify.mobile_app_tablet_eg
    metadata: {}
    data:
      message: command_screen_on
    alias: Wakeup Tablet
  - alias: Switch to Frontdoor view
    action: notify.mobile_app_tablet_eg
    metadata: {}
    data:
      message: command_webview
      data:
        command: lovelace/frontdoor
  - wait_for_trigger:
      - trigger: numeric_state
        entity_id:
          - sensor.frontdoor_person_count
        for:
          hours: 0
          minutes: 0
          seconds: 5
        below: 1
    timeout:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - alias: Switch to Frontdoor view
    action: notify.mobile_app_tablet_eg
    metadata: {}
    data:
      message: command_webview
      data:
        command: lovelace/default_view
mode: single

kr0ner avatar Jun 01 '25 07:06 kr0ner

I believe I have the same problem and have some hopefully useful information to add regarding the crashing, I have noticed that every time I run command_webview, the app re-launches just like you say, and also gets progressively slower each time you use command_webview until the app is functionally unusable. To test this, I:

  1. Went into Android settings and force stopped the app
  2. Opened the app and recorded how long to fully load the dashboard
  3. Ran the following yaml in dev tools -> actions
action: notify.mobile_app_downstairs_tablet
data: 
  message: command_webview
  data:
    command: /lovelace/0

I repeat the process a few times as I want to demonstrate that it's not a network issue but rather a performance one that gets progressively worse each time I run command_webview

  1. Force stop -> Open. 8 seconds
  2. command_webview -> 9 seconds
  3. command_webview -> 13 seconds
  4. command_webview -> 63 seconds
  5. command_webview -> Unable to connect to Home Assistant error.
  6. Force stop -> Open. 7 seconds.
  7. command_webview -> 7 seconds
  8. command_webview -> 28 seconds
  9. command_webview -> DNF after 60 seconds
  10. command_webview -> Unable to connect to Home Assistant error.
  11. Force stop -> Open. 8 seconds.
  12. command_webview -> 10 seconds
  13. command_webview -> 16 seconds
  14. command_webview -> 31 seconds
  15. command_webview -> DNF after 60 seconds
  16. command_webview -> DNF after 60 seconds
  17. command_webview -> Unable to connect to Home Assistant error.
  18. Force stop -> Open. 8 seconds.

This essentially makes command_webview unusable, as you can only really use it 1-2 times before the app will start to become unusable and require manual force stopping.

Hopefully this information helps with debugging :)

Azelphur avatar Jun 01 '25 08:06 Azelphur

Just a tips for anyone that would take this bug to fix. We recently introduce a new command in the external bus navigate that basically move the current WebView to the right place.

https://github.com/home-assistant/android/pull/5324

TimoPtr avatar Jun 01 '25 09:06 TimoPtr

Thats going to be better than command_webview because eventually the issue with command_webview using teh suggestions above is that the pages stop navigating after some navigation. Especially if you do a lot of back and forth from view A to B to C to A to C to A to B etc... it eventually gets stuck and doesnt go to a new view and only loads view A. Only loading a new webview instance reliably navigates to a new view regardless of users path. There is some history to this issue if you search the issues and PRs

dshokouhi avatar Jun 01 '25 15:06 dshokouhi

Is there any documentation for triggering the navigate command on the "external bus"? I can't find reference to that in the companion docs at all.

Related to this, whatever solution is implemented should also be verified to work when the Android app is set as the home launcher. It's hard to tell what is going on, but things seem to get confused and immediately crash when trying to manually navigate the new instance when there is one running as the launcher. It just disappears (which is probably better than being left running), but to the end user, it looks like it navigated to the wrong place.

dotorg avatar Aug 23 '25 13:08 dotorg