Reputation: 3496
I am a total beginner to android-development so please bear with me on this.
This is my sad attempt at writing a simple "Whack-a-Mole"-game. The UI consists of 9 buttons with no caption. A timer randomly picks one of the buttons that has to be clicked by the user (button gets caption "X"). If the user picks the correct button he gets 10 points. If no button (or the wrong button) is clicked in time, 10 points will be subtracted from the score.
As you can see I add an OnClickListener to each button and delete its initial caption ("Button1", "Button2" ...) in the onCreate-Method. Everything works seemingly fine, until I leave the app and then come back to it. All buttons suddenly have their initial caption again. The timer still runs (score is reduced every second) but none of the buttons change anymore.
Now I know this probably has something to do with the activity lifecycle and I already read about it. Unfortunately my English isn't the best and I'm having a bit of trouble understanding the concept. Maybe someone is willing to explain this to me in simple terms?
public class MainActivity extends Activity {
private static int[] buttons = {R.id.button1, R.id.button2, ...};
private static List<Button> buttonlist = new ArrayList<Button>();
private static TextView scoreboard;
private static Timer timer = new Timer();
private static int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreboard = (TextView) findViewById(R.id.textView1);
for (int i = 0; i < 9; i++) {
buttonlist.add((Button) findViewById(buttons[i]));
buttonlist.get(i).setText("");
// add OnClickListener for each Button
((Button) buttonlist.get(i)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//if correct Button is pressed add 20 to score
if ("X".equals(((Button) v).getText().toString())) {
score += 20;
//and set button-text to "" again ...
((Button) v).setText("");
}
else {
score -= 10;
}
}
});
}
// Start Game
timer.schedule(new MyTimerTask(), 0, 1000);
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
score -= 10;
scoreboard.setText("Your Score: " + score);
//clear buttons
for (int i = 0; i < 9; i++)
buttonlist.get(i).setText("");
//pick random button as next target
buttonlist.get((int) (Math.random() * (9 - 1) + 1)).setText("X");
}
});
}
}
@Override
protected void onResume() {
super.onResume();
// Do something here?
}
...
Upvotes: 0
Views: 172
Reputation: 49986
Once you leave your activity, system will call following methods in your activity:
onPause onStop onDestroy
your activity class instance will be destroyed
when you enter your activity again it will be created again, following methods will be called:
onCreate onStart onResume
this is how activity lifecycle looks like in your case (there are a bit more details)
your problem is that your buttonList is static, together with timer and score. Even though you leave your app, system will not destroy your app process and this way all static variables will stay unchanged. This means your buttonlist will contain initially buttons from previous activity instance.
solution is to not use static keyword. If you want to retain some state after exiting activity look into onSaveInstanceState. Also - assuming your device can rotate, this will also recreate your activity.
Upvotes: 1