I'm building one of my first android apps. I met this problem that everytime you change screen orientation, Android destroys the app and builds it up again (I know this is more complex, but this isn't the subject of this post.)
I am wondering if it is good idea to have a Game game
variable inside my MainActivity.java
that would simply implement Serializable
. This way I can do this:
public class MainActivity extends AppCompatActivity {
private MagicSquareGame game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
game = MagicSquareGame.getEngine();
initEditTextReferences();
initButtonReferences();
updateSumResultsOnScreen();
}
(all the other stuff...)
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable("game", game);
Log.i("MSG", "saving Game");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
game = (MagicSquareGame) savedInstanceState.getSerializable("game");
updateSumResultsOnScreen();
Log.i("MSG", "loading Game");
}
Is this an okay approach? Code smell? What problems can I meet?
Parcelable
instead ofSerializable
because a) it's made for this purpose and b) performance bec ause a). \$\endgroup\$Application
which lives (at least) as long as the app runs. \$\endgroup\$