Tuesday, 20 August 2013

runOnUiThread causing app slow-down and eventually force-close

runOnUiThread causing app slow-down and eventually force-close

I have MainActivity (which extends Activity) and GameView (which extends
SurfaceView).
MainActivity has TextViews for score and for time left. GameView
calculates score and time left, and updates the TextViews in MainActivity.
This works wells, but the problem is when I re-start the game over and
over. The more games are re-started, the slower they become and eventually
the app force-closes.
Here's my MainActivity.
public class MainActivity extends Activity {
/* Variables are declared here */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Variables are initialized here */
mGameView = (GameView) findViewById(R.id.mGameView);
findViewById(R.id.submitButton).setOnClickListener(buttonListener);
findViewById(R.id.resetButton).setOnClickListener(buttonListener);
// TextViews for displaying Score and Timer
tvScore = (TextView) findViewById(R.id.totalScore);
tvTimer = (TextView) findViewById(R.id.timer);
}
@Override
protected void onDestroy() {
super.onDestroy();
/* Garbage collection by unbindDrawables() here */
}
//---------------------------
// Option menu
//---------------------------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 0, "New Game");
menu.add(1, 2, 0, "Quit Game");
return true;
}
//---------------------------
// onOptions ItemSelected
//---------------------------
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Intent intentNewGame = new Intent(MainActivity.this,
MainActivity.class);
MainActivity.this.startActivity(intentNewGame);
finish();
break;
case 2:
/* Quits game here */
break;
}
return true;
}
//---------------------------------------------
// buttonListener()
//---------------------------------------------
private OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.submitButton) {
mGameView.submit = true;
} else if (id == R.id.resetButton) {
mGameView.reset = true;
}
}
};
//---------------------------------------------
// SetTotalScore()
//---------------------------------------------
public void SetTotalScore(final int totalScore) {
final String score = "" + totalScore + " points";
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
tvScore.setText(score);
}
});
}
//---------------------------------------------
// SetTimer()
//---------------------------------------------
public void SetTimer(final String timer, final int time) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
if (time <= 10) tvTimer.setTextColor(Color.RED);
tvTimer.setText(timer);
}
});
}
}
And here's how I call SetTotalScore() and SetTimer() from GameView in
order to update the TextView with new score and remaining time.
((MainActivity) getContext()).SetTotalScore(totalScore);
((MainActivity) getContext()).SetTimer(timer, timeLeft);
Again, this works well but the things is every time I start a new game, it
gets slower and slower until it eventually force-closes.
Any good idea to handle this will be highly appreciated!
Thank you!

No comments:

Post a Comment