Search code examples
androidandroid-activityback

Ingame goback not working


Ok my game starts with 2 buttons: One is the new game with this code:

    case R.id.newgame:
        openNewGameDialog();
        break;

    private void openNewGameDialog() {
    new AlertDialog.Builder(this)
    .setTitle("new game")
    .setItems(R.array.difficulty,
     new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialoginterface,
              int i) {
           startGame(i);
        }
     })
    .show();
}




   private void startGame(int i) {
    Log.d(TAG, "clicked on " + i);
    Intent intent = new Intent(SudokuActivity.this, Game.class);
    startActivity(intent);
    intent.putExtra(Game.KEY_DIFFICULTY, i);

}

Ok,now the game has started.Let's assume I play it 2 minutes,and then decide to go on the menu,so I press the BACK button on my phone.The menu appears,but now I wanna go back to my game and continue from where I left off.

I tried to put finish(); on the continue button,but when I press it,it just gets me to the main android screen.


Solution

  • In Game Activity you must override onPause method, and call you save logic. You may use SQLite, or file, to store activity state. And in onCreate or onResume of Game load this data(if exist) and resume game. It`s correct work even if main activity crashed, or user push Home btn.