【问题标题】:how to close gracefully a kivy app on Android?如何优雅地关闭 Android 上的 kivy 应用程序?
【发布时间】:2017-03-18 16:10:08
【问题描述】:

我有一个 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() 方法似乎被调用了两次。

感谢您的想法。

【问题讨论】:

    标签: android python python-2.7 kivy


    【解决方案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()
    

    【讨论】:

    • 感谢@Juggernaut,但不幸的是它并没有改变任何东西。我仍然收到对话框。
    • 在我的 htc 愿望 525 上的 API 19 上没问题
    • 感谢@Juggernaut。我也在使用 API 19。我认为我的问题可能是由于我在 on_stop 方法中执行了一些任务,例如写入数据库。好吧,我不确定;这只是一个想法。你用的是什么版本的python?我正在使用python2.7。
    • 这里的python版本相同。你有没有花时间看 adb logcat ?
    • logcat 中没有出现什么特别的地方,只是好像调用了两次 on_stop 方法!我将编辑我的问题以添加 logcat 的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2016-06-09
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多