Android game doesn't run every other time?

Started by
2 comments, last by n3oplasm 11 years, 5 months ago
I'm writing an android game and am having a problem when running it from the launcher.

if i start it from eclipse it works every time, but the next time i go to run it from the launcher I just get an unresponsive white screen(probably just my blank surfaceview, or maybe it's not even displaying the surfaceview and it's just my blank activity). After I run it once on the launcher and wait for it to crash and force close, the next time I run it it works fine. The next time after that that I run it from the launcher, I get the white screen again, and the next time it runs fine and so on.

I'm guessing I'm not handling my thread/activity lifecycle correctly, I could be wrong tho as I am very new to android.

heres code for my start activity:

public class GameActivity extends Activity {
GameView game;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("goin good so far");
SpriteManager.man.setAssets(this.getAssets());
game = new GameView(this);
setContentView(game);
//game.start();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public void onPause() {
super.onPause();
game.pause();
}

public void onResume() {
super.onResume();
game.resume();
}
}



and my start/resume/pause methods in the game class

public void start() {
running = true;
thread.start();
}

public void resume() {
if (!running)
start();
}

public void pause() {

running = false;
while(true) {
try {
thread.join();
} catch (InterruptedException e) {
// retry
}
}
}

running is a volatile boolean. thread is just my main game thread. Right now my code is a little weird because I was messing around with the activity lifecycle trying to fix my problem so theres so random If's and things commented out.

I was hoping someone might be able to point me in the right direction or even help me figure out what to ask google as I haven't been able to find anything relevant to my issue with searches.

thank you, lemme know if you need more info or have questions about my app
Advertisement
Im thinking i have a problem with pausing/resuming my app. the reason it works every other time is because the app totally crashes and is terminated half the time, then the time after that it starts fresh and runs ok. also when installing it from eclipse the app is reinstalled then started for the first time.

I have the same white screen problem when pausing the activity with the home button and then trying to start it again from the launcher. I checked and onResume is not called when the activity is opened from the launcher for some reason, whether it was paused by the home button or from the back button. onResume is only called when the activity is first run or when it is restarted after a crash. From what I read this is not the default behavior, anyone have any idea why onResume isn't getting called?
Ok so i figured out then when I pause my activity, it gets stuck in an infinite loop calling thread.join over and over again. which is pretty damn obvious because its in a while(true) loop with no way to get out, but im confused because that code was taken from an android game programming book and works in the example game from the book. Weird.


Im just gonna remove the while loop, but then how am I gonna know if the thread was succesfully stopped? is there any flag I can use instead instead of just true in the while loop to check if it was succesfully joined?
What this loop is really doing is attempting the call to thread.join() repeatedly until it is successful (if an exception is thrown it is immediately caught, ignored and the loop resumes). However, in the code from your original post there is no way for the loop to exit. It continues looping infinitely even after thread.join() returns. The solution is to add a break; statement just after the call to thread.join() in the try block. This way, the loop terminates once thread.join is successful.

[source lang="java"]while(true) {
try {
thread.join();
break;
} catch (InterruptedException e) {
// retry
}
}[/source]

This topic is closed to new replies.

Advertisement