kivy / python-for-android

Turn your Python application into an Android APK
https://python-for-android.readthedocs.io
MIT License
8.33k stars 1.84k forks source link

Using foreground services will cause wired behaviour on Android 8 #2641

Closed ChihayaK closed 2 years ago

ChihayaK commented 2 years ago

Checklist

Versions

Description

When using the foreground service, quitting an app with the 'back' button will cause the app to shut down its kivy activity. But not the services behind them. If I relaunch the app in this state, some devices will display that the app has stopped responding while actually the entire app runs perfectly fine. So it is in a situation where the Android system thinks the app has stopped running (and displays a dialog to block the app itself), but it is not the case.

I tested the same app on LGv20 running Android 8 and LGv20 running Android 11. The Android 11 one seems to be working fine. 

Here is the screenshot of that behavior: stopped

RobertFlatt commented 2 years ago

I think this might be two Kivy issues:

The first is : on a back button/gesture Kivy stops the app on all platforms, however the Android expected default behavior is to pause the app. An app generated pause should also call mActivity.moveTaskToBack(True) after Kivy pause operations. https://github.com/kivy/kivy/blob/master/kivy/core/window/__init__.py#L1990-L1994

The second is ( see https://kivy.org/doc/stable/guide/basic.html#kivy-app-life-cycle ) a Python stop terminates the app, but does not remove the app from the task list, So some versions of Android may find the app state ambiguous. (I think, this issue is in practice only exposed by the first issue) A stop could address this with mActivity.finishAndRemoveTask()

As you point out there is an interaction with a Service. Presumably / hopefully fixing the first Kivy issue would address most cases of the issue you see, and not break p4a's service implementation. Finally there is the case of re-attaching a running Service during on_start(), I don't understand this well enough to comment.

ChihayaK commented 2 years ago

The latest p4a seems resolve this issue.