Reputation: 1042
I have a quiz game with 15 levels. After each level I present a popup with some info (game level, and some level rules). After level 15, a present final popup with info on how many points user has won. I use startActivityForResult to open the popups and send some extras and it works fine, until the last popup. After the last game, it should open the final popup with the overall score. But it doesn't do it properly. It opens on of my level popup, but with all the info filled with zeros. I don't know where it comes from. After I press OK, it opens final popup, but instead of overall score, I get 0. Here's the code:
Intent i = new Intent(this, Popup_nivoi.class);
nivo++;
REQUEST++;
switch(nivo) {
case 2:
{
brojPitanja = 11;
greska = 4;
mogucnostPreskakanjaPitanja = 3;
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 5:
{
brojPitanja = 14;
greska = 3;
mogucnostPreskakanjaPitanja = 2;
i.putExtra("brojPitanja", brojPitanja);
i.putExtra("vreme", 85);
i.putExtra("pravoNaGreske", greska);
i.putExtra("nivo", nivo);
i.putExtra("mogucnostPreskakanja", mogucnostPreskakanjaPitanja);
break;
}
.
.
.
.
case 15:
{
brojPitanja = 24;
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:
{
brojPitanja = 25;
greska = 0;
mogucnostPreskakanjaPitanja = 0;
i.putExtra("brojPitanja", brojPitanja);
i.putExtra("vreme", 30);
i.putExtra("pravoNaGreske", greska);
i.putExtra("nivo", nivo);
i.putExtra("mogucnostPreskakanja", mogucnostPreskakanjaPitanja);
break;
}
case 17:
{
Intent iFinale = new Intent(Kviz.this, Finale.class);
iFinale.putExtra("UkupanSkor", brojacTacnihOdgovora);
startActivityForResult(iFinale, 0);
finish();
break;
}
}
cancelAllTimers();
startActivityForResult(i, REQUEST);
}
In my levels popup I do this to get extras:
Bundle extras = getIntent().getExtras();
if(extras != null) {
brojPitanja = getIntent().getIntExtra("brojPitanja", 0);
pravoNaGreske = getIntent().getIntExtra("pravoNaGreske", 0);
mogucnostPreskakanja = getIntent().getIntExtra("mogucnostPreskakanja", 0);
vreme = getIntent().getIntExtra("vreme", 0);
nivo = getIntent().getIntExtra("nivo", 0);
}
And my whole finale popup class:
public class Finale extends Activity implements OnClickListener{
TextView cestitamo,ukupanSkor;
Button ok;
int brojOdgovora;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.finale);
if (getIntent().getExtras() != null)
brojOdgovora = getIntent().getIntExtra("brojTacnihOdgovora", 0);
inicijalizujVarijable();
}
private void inicijalizujVarijable()
{
Typeface localTypeface = Typeface.createFromAsset(getAssets(), "crazy_moj_3.ttf");
cestitamo = (TextView) findViewById(R.id.tvCestitamo);
cestitamo.setTypeface(localTypeface);
ukupanSkor =(TextView) findViewById(R.id.tvUkupanSkor);
ukupanSkor.setTypeface(localTypeface);
ok = (Button) findViewById(R.id.bOKCestitamo);
ukupanSkor.setText("You answered correctly on " + brojOdgovora + " of 300 questions!");
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
Intent localIntent = new Intent();
setResult(RESULT_OK, localIntent);
finish();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The weird thing is that I have a popup for "time is up" and I use the same routine and the same code to send extras and to show how many points user scored and it works fine.
Upvotes: 0
Views: 81
Reputation: 4707
It opens one of my level popup, but with all the info filled with zeros.
This occurs because you put startActivityForResult(i, REQUEST)
outside the switch-case
. When nova
is 17, the Intent i
is not initialized with any extras, and it will always be called no matter what stage it is. Hence, the popup appears with all values being 0 (from the default value that you set).
After I press OK, it opens final popup, but instead of overall score, I get 0.
This is because you use different name for your extras
. When initializing the Intent iFinale
, you use UkupanSkor
for overall score. But inside the Finale activity, you use brojTacnihOdgovora
instead.
My suggestion is to use same Intent
to start the activity, and set the data based on the stage number. You only need to change case 17
. Try:
case 17:
{
REQUEST = 0; // set the request code to 0
i = new Intent(Kviz.this, Finale.class); // set the intent to open final popup instead
i.putExtra("brojTacnihOdgovora", brojacTacnihOdgovora); // set the extra name to match with the activity
finish();
break;
}
Upvotes: 2