89174

onActivityResult not working properly

Question:

So, I have this quiz game and I have bunch of popup screens (for wrong answer, for game end, for time's up and so on). My game has 15 level, each level different number of question, different time and so on. I want when I finish level 1 for example, to present popup with some info, and when a user clicks OK, I want to load next level. I did all that, except something is messed up in onActivityResult method, it always loads last case. Here's my code:

static final int MY_REQUEST = 0; Intent i = new Intent(this, Popup_nivoi.class); nivo++; nivoKraj++; switch(nivo) { case 2: { brojPitanja = 11; greska = 4; mogucnostPreskakanjaPitanja = 2; i.putExtra("brojPitanja", brojPitanja); i.putExtra("vreme", 100); i.putExtra("pravoNaGreske", greska); i.putExtra("nivo", nivo); i.putExtra("mogucnostPreskakanja", mogucnostPreskakanjaPitanja); break; } case 3: { brojPitanja = 12; greska = 4; mogucnostPreskakanjaPitanja = 2; i.putExtra("brojPitanja", brojPitanja); i.putExtra("vreme", 95); i.putExtra("pravoNaGreske", greska); i.putExtra("nivo", nivo); i.putExtra("mogucnostPreskakanja", mogucnostPreskakanjaPitanja); break; } case 4: { brojPitanja = 13; greska = 3; mogucnostPreskakanjaPitanja = 2; i.putExtra("brojPitanja", brojPitanja); i.putExtra("vreme", 90); i.putExtra("pravoNaGreske", greska); i.putExtra("nivo", nivo); i.putExtra("mogucnostPreskakanja", mogucnostPreskakanjaPitanja); break; } . . . . case 15: { brojPitanja = 23; greska = 0; mogucnostPreskakanjaPitanja = 0; i.putExtra("brojPitanja", brojPitanja); i.putExtra("vreme", 35); i.putExtra("pravoNaGreske", greska); i.putExtra("nivo", nivo); i.putExtra("mogucnostPreskakanja", mogucnostPreskakanjaPitanja); break; } case 16: { Intent iFinale = new Intent(this, Finale.class); startActivityForResult(iFinale, 6); break; } } cancelAllTimers(); startActivityForResult(i, MY_REQUEST);

This is what I do in Popup class, on OK button pressed:

ok.setOnClickListener(new OnClickListener() { public void onClick(View paramAnonymousView) { Intent localIntent = new Intent(); setResult(RESULT_OK, localIntent); finish(); } });

And this is where it starts to get weird, back in my game class (the first thing with MY_REQUEST is for wrong answer popup):

protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == MY_REQUEST) { if(resultCode == RESULT_OK) { nextQuestion(); } } else { switch(requestCode) { case 0: if(resultCode == RESULT_OK) { nextQuestion(); timer.start(); break; } case 1: if(resultCode == RESULT_OK) { nextQuestion(); timer1.start(); break; } case 2: if(resultCode == RESULT_OK) { nextQuestion(); timer2.start(); break; } case 3: if(resultCode == RESULT_OK) { nextQuestion(); timer3.start(); break; } } } }

I don't get it why, but it's always goes to case 3.

Answer1:

first change this: startActivityForResult(i, 1);

to this: startActivityForResult(i, MY_REQUEST );

When you put 1 , its mean it will always be 1 when you return from the next activity. You should use different request code for each case

Your code should be like this: protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

if(requestCode == MY_REQUEST){ if(resultCode == RESULT_OK){ nextQuestion(); } }else{ switch(requestCode) { case 0: if(resultCode == RESULT_OK) { nextQuestion(); timer.start(); break; } case 1: if(resultCode == RESULT_OK) { nextQuestion(); timer1.start(); break; } case 2: if(resultCode == RESULT_OK) { nextQuestion(); timer2.start(); break; } case 3: if(resultCode == RESULT_OK) { nextQuestion(); timer3.start(); break; } } } }

if you dont break, the case will move to the next one until it end or find breaks.

Answer2:

You have the RequestCode hardcoded to 1 in your StartActivity !! This is why OnActivityResult() always matches 1.

startActivityForResult(i, 1);

Answer3:

You forgot to put break statments thats why control comes to the last case statement everytime, you can fix this by adding it

switch(requestCode) { case 0: if(resultCode == RESULT_OK) { nextQuestion(); timer.start(); } break; case 1: if(resultCode == RESULT_OK) { nextQuestion(); timer1.start(); } break; case 2: if(resultCode == RESULT_OK) { nextQuestion(); timer2.start(); } break; case 3: if(resultCode == RESULT_OK) { nextQuestion(); timer3.start(); } }

Recommend