0

I'm trying to understand finishing an activity and starting another.

<activity
            android:name="com.blabla.game.OyunActivity"
            android:label="@string/title_activity_oyun"
            android:noHistory="true" >
        </activity>

OyunActivity :

int number = 1;
while(true)
{
if(number == 52)
{
            Intent intent = new Intent(this, GameOver.class);
            startActivity(intent);
            finish();
}
number++;
Log.d("TAG", number);
}

It's starting GameOver activity but OyunActivity not finishing. It's keeping increase number variable and outputting it to Logcat.

PS : Actually my code not really stupid like this. I'm trying to make a basic game. It should stop and open GameOver activity when number = 52

Eray
  • 7,038
  • 16
  • 70
  • 120

2 Answers2

0

Should be break instead of finish()

 int number = 1;
while(true)
{
if(number == 52)
{
            Intent intent = new Intent(this, GameOver.class);
            startActivity(intent);
            return;
}
number++;
Log.d("TAG", number);
}
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • this time it will break only while loop, and continue from outside loop? – Eray Jul 29 '13 at 20:39
  • 1
    It will continue to finish al the code in the method and then GameOver will be launched. If you do not want to continue with other code use return instead of break. – Hoan Nguyen Jul 29 '13 at 20:43
  • Probably return; will solve my problem. I'll try and share results. – Eray Jul 29 '13 at 20:59
  • solved. @Hoan can you edit your answer please. I'll accept it. Thank you. – Eray Jul 29 '13 at 21:04
-1

This question will be helpful for you, I think.

Quotation :

You can use finish() method or you can use:

android:noHistory="true"

And then there is no need to call finish() anymore.

<activity android:name=".ClassName" android:noHistory="true" ... />
Community
  • 1
  • 1
TN888
  • 7,659
  • 9
  • 48
  • 84