arnaugamez
arnaugamez

Reputation: 23

Kivy app running on android closes after the screen locks. How to avoid it?

I have recently started with kivy framework. I've just made a simple calculator app. I have became to make an .apk and run it on android, but I have a problem.

When the screen locks both manually and because of the time, the app closes automatically, losing all the data (numbers) that I had introduced beforeand so I have to start it another time and reintroduce the numbers.

My question here is if you know some way to avoid this. If it is requiered a piece of code on the python code or on the kv language file. I don't know, but I want that when the screen locks and I unlock it, the app preserve its previous status.

PS: I have tried also starting the app from kivy launcher, without the previous .apk compiling, but I have got the same result, so I think that it would be a trouble of the python code or the kv language code, not a trouble in the process of packaging the app with the buildozer tool.

Upvotes: 1

Views: 2227

Answers (1)

inclement
inclement

Reputation: 29450

You need to implement an on_pause method for your app, as described in the documentation.

The example from the kivy docs is

class TestApp(App):

   def on_pause(self):
      # Here you can save data if needed
      return True

   def on_resume(self):
      # Here you can check if any data needs replacing (usually nothing)
      pass

Note that you are never guaranteed that your app state will be preserved - android may kill it without warning while it is in the paused state. Make sure you save any data you need in the on_pause method.

Upvotes: 5

Related Questions