1

I need to do some actions before quit (save settings, etc.), I do it with Button widgets, but if I press on close button of the window, it just closes. Maybe I should handle App's on_close() event somehow? Then I don't know how to send data from Layout to App. For now I have smth like:

def quit_game(self):
        # Saving different data.
        ...
        quit()

What should I do?

Necronomicron
  • 1,150
  • 4
  • 24
  • 47

1 Answers1

3

Yes, you should handle the on_stop method on your App class by implementing the appropriate method.

Example:

class MyApp(App):
    ...
    def on_stop(self):
        # do what you need to here
        value_to_save = self.root.subwidget.value
        # self.root is your root widget - either the 
        # widget you return from App.build() or
        # the root widget in your app kv file

Keep in mind, also, that if you plan to develop for Android you will need to implement on_pause as well. After an Android app is paused, it can be killed without warning by the OS and your on_stop method will not be called.

kitti
  • 14,663
  • 31
  • 49
  • It seems it works now, but some issue. I have string `on_release: app.stop()` in *.kv, so this string calls `on_stop()` twice, what do I wrong? And what should I type in `on_pause()`? The same as in `on_stop()`? – Necronomicron Aug 11 '14 at 21:49
  • 1
    I would suggest doing all of your saving in a new method (i.e. `save_game()` or similar) and call that method from both `on_pause()` and `on_stop()`. – kitti Aug 11 '14 at 22:14
  • I thought so. What about double call? – Necronomicron Aug 11 '14 at 22:19
  • 2
    The `on_stop` being dispatched twice is a bug in Kivy. You can work around this for now by using `stopTouchApp()` with a kv import. I submitted this bug with a workaround here: https://github.com/kivy/kivy/issues/2397 – kitti Aug 11 '14 at 22:24