Mihai
Mihai

Reputation: 3

Pausing the previous activity

I created an activity that appears when game is over with the option of restarting the game. I want to return to the game activity using finish() method instead of calling again the activity with startActivity(), but I don't know how to pause the activity or where to call the restartingGame() method when I return to the game activity. Is it possible to do this or should I call again the game activity using startActivity() ?

Upvotes: 0

Views: 45

Answers (1)

Victor Isarov
Victor Isarov

Reputation: 43

You can start the new activity for result (from the gaming activity):

static final int REQUEST_CODE = 1;  // The request code
private void startFinishActivity() {
    Intent intent = new Intent(this, FinishActivity.class); 
    startActivityForResult(intent, REQUEST_CODE);
}

Now in the FinishActivity when a user chose whether to restart the game return an intent which contains information about the gaming result (Restart/End):

private void endFinishActivity(bool shouldRestart) {
    Intent returnIntent = new Intent();
    returnIntent.putExtra("restart", shouldRestart);
    setResult(Activity.RESULT_OK,returnIntent);
    finish();
}

Then in the Gaming activity you need to override onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {

            bool shouldRestart = data.getBooleanExtra("restart");
        }
    }
}

And now you should call restartGame or endGame.

Upvotes: 2

Related Questions