Restarting my application


Ask by : Nadav November 23, 2012 02:00

I'm building a small game with 3 "lives" and a Timer that goes down. When the lives are finished or when the timer is at zero I told it to go to a "game over" activity. In the game over I have a "try again" button that sends you to the Welcome activity and you can start again. The problem is I think the Timer from Previous game is still running as it goes to gameover activity on the next games even if the timer and lives are ok.

any way i can reset the app to the start mode when someone presses "try again"?

Thanks :)

Edit: my code: this is when the time ends and it goes to gameover activity,

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    ImageView iv =(ImageView) findViewById(R.id.imageView1);
    final TextView mTextField = (TextView) findViewById(R.id.textView2);
    CountDownTimer one = new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("time: " + millisUntilFinished / 1000);
     }

     public void onFinish() {

         toGameOver(null);

     }
  }.start();

}




public void toGameOver (View view)
{
    Intent intent = new Intent(this,GameOverActivity.class);
    intent.putExtra(EXTRA_MESSAGE, points.toString());
    startActivity(intent);

}

this is when the lives end and it goes to gameover activity:

public void changepic(View view)
{
    boolean isCorrect=false;
    int n = 8;
    ImageView iv =(ImageView) findViewById(R.id.imageView1);
    TextView tv = (TextView) findViewById(R.id.textView1);  
    if(rndInt<3&&rndInt>0){
        isCorrect=true;
        points++;
    }

    if(!isCorrect&&lives==0)
        toGameOver(null);
    if (!isCorrect&&lives==1&&rndInt!=0)
    {
        lives--;
        ImageView iv1 = (ImageView) findViewById(R.id.imageView3);
        iv1.setVisibility(View.INVISIBLE);
    }
    if (!isCorrect&&lives==2&&rndInt!=0)
    {
        lives--;
        ImageView iv1 = (ImageView) findViewById(R.id.imageView4);
        iv1.setVisibility(View.INVISIBLE);
    }
    if (!isCorrect&&lives==3&&rndInt!=0)
    {
        lives--;
        ImageView iv1 = (ImageView) findViewById(R.id.imageView2);
        iv1.setVisibility(View.INVISIBLE);
    }

and this is in game over activity when i want it to go the start:

public void goToStart (View view)
{
    Intent intent = new Intent(this,WelcomeActivity.class);
    startActivity(intent);

}

sorry for not posting it when asking

View original question

Answer by : Dino ZarafonitisNovember 23, 2012 02:00

If you want to stop the timer when the game is over, then inside the function toGameOver() you need to call one.cancel() That will result to the cancellation of the timer. Then when the game restarts , you should be calling one.start()

View original answer