You should initialize pos
in the resume
and show
methods from the saved preferences file. Something like:
Preferences prefs = Gdx.app.getPreferences("my-preferences");
pos.x = prefs.getFloat("X", 99.0f); // XXX use a better default
pos.y = prefs.getFloat("Y", 99.0f); // XXX use a better default
If you compute reasonable default values, you won't have to worry about the initial case.
Technically, you could just do this at the top of render
(in the usual case you'll just re-read the values you just saved on the previous render, but in the case where this is the first render
call it should help). But that seems a bit wasteful (and doing unnecessary stuff on the render thread, especially IO, is a bad idea).
It is a little excessive to flush
on every render, and will lead to the occasional hiccup when the IO stalls your render thread. You should be able to just flush
the preferences object in pause
, as your app is on its way out. (You'll have to keep a reference to the Preferences
object on your instance of course.)
Your other questions at the end of this question should be posted as a separate question. Before you do that, please be sure to read http://developer.android.com/guide/topics/data/data-storage.html