Import/Export for LogMyLife #androiddev

Over the past few nights I whipped out an import/export capability for LogMyLife. Right now all it does is basically package up the database and a version file; later I expect I’ll include saving properties, which is why I’m including format version in the archive (I thought that was good foresight, as who knows what else might change). I wrapped it up in an activity you can call from the menu of the Main activity.

The implementation is reasonably straightforward, although I didn’t know a thing about handling files from Java. It’s predictably verbose but not too outrageous. I got a lot of help from this StackOverflow entry and this sun.com article. If you’re thinking of doing this you might check out how I did it for inspiration. I’d certainly encourage everyone to provide this capability so users can back up and transfer their app data!

I faced an odd little problem on importing: of course, it replaces the app’s DB. When I returned to the main activity, I returned a result code indicating this had happened, and had it close the database handle and reinitialize the components that displayed information from the database. But something seemed to be holding on to a database cursor and requerying it, and I couldn’t quite figure out why that was happening. Eventually I decided it would probably be simplest if I just have the activity restart itself from scratch. An answer on StackOverflow helped with how to do that – it’s not totally obvious; basically, just finish() the activity, and then (before exiting the same method) start it again.

Gingerbread is out, woo! And the Nexus S is coming; but I want a phone with a keyboard, dammit. Once my Nexus One gets 2.3 I’ll likely try to sell it in favor of the G2. Even though tethering is extra, which is wrong and bad and to be vehemently discouraged, but I don’t use tethering anyway.