1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
from android.runnable import run_on_ui_thread # noqa
from android.activity import _activity as act # noqa
from webview.platforms.android.base import EventLoop
from webview.platforms.android.event import EventDispatcher
class App(EventDispatcher):
# Return the current running App instance
_running_app = None
def __init__(self, **kwargs):
App._running_app = self
super().__init__(**kwargs)
self._eventloop = EventLoop()
#: The *root* widget returned by the :meth:`build_view`
self.root = None
def build_view(self):
"""Initializes the application; it will be called only once.
If this method returns a widget (tree), it will be used as the root
widget and added to the window.
:return:
None or a root :class:`~android.widget.RelativeLayout` instance
if no self.root exists."""
return self.root
@run_on_ui_thread
def set_content_view(self, root):
act.setContentView(root)
def on_create(self, activity, saved_instance_state):
"""Event handler for the `on_create` event which is fired after
initialization (after build() has been called) but before the
application has started running.
"""
def on_pause(self, activity):
"""Event handler called when Pause mode is requested"""
def on_destroy(self, activity):
"""Event handler for the `on_destroy` event which is fired when the
application has finished running (i.e. the window is about to be
closed).
"""
def on_resume(self, activity):
pass
def on_start(self, activity):
pass
def on_stop(self, activity):
pass
def run(self):
self.build_view()
self._eventloop.status = 'created'
self._eventloop.mainloop()
@staticmethod
def get_running_app():
"""Return the currently running application instance."""
return App._running_app
def pause(self):
"""Pause the application.
On Android set OS state to pause, Kivy app state follows.
No functionality on other OS.
.. versionadded:: 2.2.0
"""
act.moveTaskToBack(True)
def stop(self):
self.dispatch('on_destroy')
self._eventloop.close()
App._running_app = None
|