Search code examples
androidwear-os

How to save data on wear os android in standalone app


Hi on android apps I used onSaveInstanceState to persist my data when I rotate the phone or exit app. Now I'm making Wear Os for smartwatch app that will be stand-alone but will be also able to connect to my app on the phone. So I manage to connect a phone and wear app with DataClient but my problem is when I start the app on the watch, get some score and then exit the app by swiping from left to right my app my score is reset to zero. Interesting is that the score is saved if I exit the app with the power button but I don't think that has to do anything with my onSaveInstanceState.

I tried to use onSaveInstanceState in wear app but it does not work. When I tried to debug it is not called. I guess it is not supported to work on wear and that I have to, somehow, use DataClient API for this but I don't know how to use it for this purpose. This is all new to me and I'm still a beginner. I only manage to send images from a phone app to wear app but I want to be able to save data without phone connected and to retrieve it.

I just want to save data from my UI so when a user comes back to my app that data is not lost. So need some simple sample code like saving some int or string and retrieving it when the app is started again.


Solution

  • I decided that for me the best is to use Kotlin Room. I needed to save my data anyway in database so this way I just save it every time the data is changed. Maybe more efficient would be to save it just in onPause or onStop but I don't have a lot of data so this works for me.