Currently I have a simple game where sprites are initialised onto the game and when they are pressed then the score is incremented by one. This is done in the game class where I have this code to pass the number from hitCount on:
public int getHitCount() {
return hitCount;
}
Next I have this code which is supposed to recieve the hitCount from the getHitCount and will then pass it on to the mainmenu. The unbindservice is for the music player and not related to the intent:
public void finish(){
super.finish();
Intent returnIntent = new Intent();
returnIntent.putExtra("GAME_SCORE",(gameView.getHitCount()));
setResult(RESULT_OK, returnIntent);
doUnbindService();
}
This is the main menu class showing the activity has been started to get the result for the intent and where the intent is supposed to be received but for some reason it is not getting a value.
public void startGame(View v){
gameIntent = new Intent(this,GameActivity.class);
startActivityForResult(gameIntent, SCORE_REQUEST_CODE );
}
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");
tvhigh.setText(Integer.toString(scoreFromGame));
}
}
}
}
I'm not sure why no values are being passed because as far as I can tell my intents are correct and the data should be passed.
Aucun commentaire:
Enregistrer un commentaire