0

I am making a simple app to learn Android at the same time, I am very much a newbie.

So far this is what i have:

the app opens into levels.java which has 8 buttons (in an xml file), when button 8 is clicked,I have this code:

public void button_clicked8(View v) {
        startActivity(new Intent(this, GameScreen.class));
}

which launches my gamescreen activity, after playing my simple addition game, I have a button that says "end" which calls a function with a finish() in it, it also sets a variable gameover=true; , this sends me back to levels.java

but if I press the button 8 again, it does send me to Gamescreen but I find that gameover=true; is still set :(

Why is that? How do I start the activity "fresh"?

Thanks!

Ryan
  • 9,821
  • 22
  • 66
  • 101
  • This details don't really make a good explanation, add some more details. – Lalit Poptani Sep 09 '11 at 02:50
  • Basically, I do a finish and go to the levels.java screen, then i try to go back to the gamescreen and whatever variables I had set are still set :( – Ryan Sep 09 '11 at 02:54
  • Thanks guys! Voted everyone up as everyone had something that was useful to me but selected the asnwer that I used. – Ryan Sep 09 '11 at 03:04

6 Answers6

1

I suggest your read my answer to another mildly similar question. Basically, finish is exactly the same as the user hitting the back button. It's more-or-less up to you as the developer to change the state based on the interaction from the user (ie via the intent).

For example you could do something like the following...

Intent intent = new Intent();
Bundle bun    = new Bundle();

bun.putBoolean("newGame", true);

intent.setClass(this, GameActivity.class);
intent.putExtras(bun);
startActivity(intent); 
Community
  • 1
  • 1
Andrew White
  • 52,720
  • 19
  • 113
  • 137
1

I guess you don't want to start the gamescreen when gameOver is true

 public void button_clicked8(View v) {
            if(gameOver) return;
            startActivity(new Intent(this, GameScreen.class));
    }
Rasel
  • 15,499
  • 6
  • 40
  • 50
1

Use flag for intent when you start Activity.

teddy
  • 197
  • 2
  • 15
0

How about reseting gameover value in onCreate() method of GameScreen?

romy_ngo
  • 1,061
  • 8
  • 15
0

Please read about activity lifecycles here and as far as setting your gameover variable another way to store the value is with SharedPreferences. Do it like this:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("gameover", true);
editor.commit();

and to read your value, do it like this:

Boolean gameOver = sharedPreferences.getBoolean("gameover", true);

By doing this you are permanently setting your value and will not have to worry about activity lifecycle events.

nickfox
  • 2,835
  • 1
  • 26
  • 31
0
u can declare a static boolean variable gameOver in levels.java.

which u can modify from end button click in GameScreen.java. when finish has been called all d variables r destroyed. u can override GameScreen.java's onCreate(),onResume(),onDestroy(),onStart(),onPause(),onStop() to handle ur variable

tasnim
  • 21
  • 1