Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:41:52+00:00 2026-06-17T05:41:52+00:00

I am a total beginner to android-development so please bear with me on this.

  • 0

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?
}


...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:41:53+00:00Added an answer on June 17, 2026 at 5:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total beginner at iPhone development so I have a little question: This
I'm a total beginner in C programming so please bear with me. I have
I am a total beginner in JS/XML. I have this simple code that needs
I am a total beginner to d3.js so please be kind :) considering this
I'm a total beginner so I apologize if this question is elementary. I have
Please understand: I am a total beginner at Actionscript and anything like Actionscript, and
Total noob here. This is my first c# attempt, its a console application that
Let me preface this post with a single caution. I am a total beginner
This may be a total beginner's question, but I have yet to find an
I am a total beginner in PHP and writes simple web apps in procedural

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.