如何在Android上优雅地关闭一个kivy应用程序?

时间:2017-03-18 16:10:09

标签: android python python-2.7 kivy

我在里面有一个kivy应用程序:

Button:
    text: "Close"
    on_release: app.stop()

这很有效。但不幸的是,当应用程序在Android上执行时,应用程序停止但不优雅,因为Android对话框打开,表示应用程序已关闭。这不是正常结局的预期行为。

所以我的问题是:如何在Android上优雅地关闭一个kivy应用程序,以便看不到这个烦人的对话框?

以下是logcat的输出:

I/python  ( 4960): ANSWERS (on_stop in App) =  None
I/python  ( 4960): screen names =  ['welcome']
I/python  ( 4960): answers have been saved on disk
I/python  ( 4960): closing the SQL connection...
I/python  ( 4960): now returning True
I/python  ( 4960): [INFO   ] [Base        ] Leaving application in progress...
I/python  ( 4960): ANSWERS (on_stop in App) =  None
I/python  ( 4960): screen names =  ['welcome']
I/python  ( 4960): answers have been saved on disk
I/python  ( 4960): closing the SQL connection...
I/python  ( 4960): now returning True
I/python  ( 4960): Python for android ended.

如您所见,App.on_stop()方法似乎已被调用两次。

感谢您的想法。

1 个答案:

答案 0 :(得分:1)

您是否覆盖了应用的on_stop()方法?试试这个。

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.app import App


Builder.load_string("""
<MyWidget>:
    BoxLayout:
        Button:
            text: "Close"
            on_release: app.stop()
""")


class MyWidget(BoxLayout):
    pass


class MyApp(App):
    def build(self):
        return MyWidget()

    def on_stop(self):
        return True

if __name__ == '__main__':
    MyApp().run()