11
2+ Working on Android
3+ ==================
4+
5+ This page gives details on accessing Android APIs and managing other
6+ interactions on Android.
7+
8+
29Accessing Android APIs
3- ======================
10+ ----------------------
411
512When writing an Android application you may want to access the normal
613Android Java APIs, in order to control your application's appearance
@@ -27,7 +34,7 @@ recommended for use in new applications.
2734
2835
2936Using Pyjnius
30- -------------
37+ ~~~~~~~~~~~~~
3138
3239Pyjnius lets you call the Android API directly from Python Pyjnius is
3340works by dynamically wrapping Java classes, so you don't have to wait
@@ -83,7 +90,7 @@ You can check the `Pyjnius documentation <Pyjnius_>`_ for further details.
8390
8491
8592Using Plyer
86- -----------
93+ ~~~~~~~~~~~
8794
8895Plyer provides a much less verbose, Pythonic wrapper to
8996platform-specific APIs. It supports Android as well as iOS and desktop
@@ -107,7 +114,7 @@ This is obviously *much* less verbose than with Pyjnius!
107114
108115
109116Using ``android ``
110- -----------------
117+ ~~~~~~~~~~~~~~~~~
111118
112119This Cython module was used for Android API interaction with Kivy's old
113120interface, but is now mostly replaced by Pyjnius.
@@ -147,3 +154,51 @@ code::
147154
148155 import webbrowser
149156 webbrowser.register('android', AndroidBrowser, None, -1)
157+
158+
159+ Working with the App lifecycle
160+ ------------------------------
161+
162+ Handling the back button
163+ ~~~~~~~~~~~~~~~~~~~~~~~~
164+
165+ Android phones always have a back button, which users expect to
166+ perform an appropriate in-app function. If you do not handle it, Kivy
167+ apps will actually shut down and appear to have crashed.
168+
169+ In SDL2 bootstraps, the back button appears as the escape key (keycode
170+ 27, codepoint 270). You can handle this key to perform actions when it
171+ is pressed.
172+
173+ For instance, in your App class in Kivy::
174+
175+ from kivy.core.window import Window
176+
177+ class YourApp(App):
178+
179+ def build(self):
180+ Window.bind(on_keyboard=self.key_input)
181+ return Widget() # your root widget here as normal
182+
183+ def key_input(self, window, key, scancode, codepoint, modifier):
184+ if key == 27:
185+ return True # override the default behaviour
186+ # the key now does nothing
187+ return False
188+
189+
190+ Pausing the App
191+ ~~~~~~~~~~~~~~~
192+
193+ When the user leaves an App, it is automatically paused by Android,
194+ although it gets a few seconds to store data etc. if necessary. Once
195+ paused, there is no guarantee that your app will run again.
196+
197+ With Kivy, add an ``on_pause `` method to your App class, which returns True::
198+
199+ def on_pause(self):
200+ return True
201+
202+ With the webview bootstrap, pausing should work automatically.
203+
204+ Under SDL2, you can handle the `appropriate events <https://wiki.libsdl.org/SDL_EventType >`__ (see SDL_APP_WILLENTERBACKGROUND etc.).
0 commit comments