Beda
Beda

Reputation: 61

Intents crashing in simple android game

I am making a simple game for android, however as I am kind of beginner sometimes I have some problems with basic things and errors in my code. I dont know what is wrong with that code but it seems to crash when I press back button and does not redirect the score from game to main menu.

public void finish(){
    Intent returnIntent = new Intent();
    returnIntent.putExtra("GAME_SCORE",gameView.getHitCount());
    setResult(RESULT_OK, returnIntent);
    super.finish();
  }

GameView:

public int getHitCount(){
    return hitCount;
    }

and MainMenu:

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
    // Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(""+Integer.toString(scoreFromGame));
            }
        }   
    }
}

Upvotes: 0

Views: 94

Answers (1)

Ahmad Al-Sanie
Ahmad Al-Sanie

Reputation: 3785

from what I've understand, the GameActivity returns the user to mainMenuActivity so first thing first the super keyword in java comes always first you can't just put anything before super() and if the game activity returns you to main menu the onFinish() method must be:

super.finish();
Intent returnIntent = new Intent(GameActivity.this,MainMenu.class);
returnIntent.putExtra("GAME_SCORE",gameView.getHitCount());
setResult(RESULT_OK, returnIntent);

and to get the game score if its integer then use:

Intent intent = getIntent();
int intValue = intent.getIntExtra("GAME_SCORE", 0);

in your MainMenu class hope this will help you.

Upvotes: 2

Related Questions