Plamen Penchev Plamen Penchev - 3 years ago 154
Android Question

Why is my application closing after starting Activity from custom View?

I am working on a simple game in Android studio and I'm calling GameOver activity, with Try Again button form the Game Activity (which is just named MainActivity). It is called when the main character collides with one of the monsters. What is my problem is that when I try to finish by

((Activity)getContext()).finish();
My application closes instead of starting the new activity. This is the method that starts it:

public void isCharacterDead(){
for (Monster item : monsters) {
if (characters.get(0).deadCollision.contains(item.collision.centerX(), item.collision.centerY())) {
player.stop();
long gameOverDelayEnd = (System.nanoTime()/1000000)+1000;
long gameOverDelayStart = System.nanoTime()/1000000;

while(gameOverDelayStart < gameOverDelayEnd){
gameOverDelayStart = System.nanoTime()/1000000;

}
characters.remove(0);

Intent gameOver = new Intent(getContext(),GameOver.class);
gameOver.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity)getContext()).finish();
getContext().startActivity(gameOver);

}
}
}


And my GameOver Activity is:

public class GameOver extends Activity {
private Button tryAgain;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//removing the title from the screen in order to have empty screen
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setContentView(R.layout.activity_game_over);

tryAgain = (Button) findViewById(R.id.tryagain);
tryAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent game = new Intent(GameOver.this, MainActivity.class);
GameOver.this.finish();

startActivity(game);

}
});
}
}


Now if i don't use
((Activity)getContext()).finish();
, it is starting the GameOver activity and when Try Again is clicked I move to new instance of the Game Activity(MainActivity). But it drops the performance on each new start via the Try Again button in GameOver. I suppose it is due to starting the new game Activity over the last one without finishing it. Any suggestion on what to do to avoid this? There's nothing inside the android monitor as an exception (My GPU monitor is disabled also).

Answer Source

try to swap these two lines:

((Activity)getContext()).finish();
getContext().startActivity(gameOver);

And do not forget to break the for loop when you start the activity

Edit:

After editing your questions adding your complete code it is now clear the reason of your problem:

You are calling the method upd from the thread you create,upd in turn calls the method isCharacterDead that is finishing the activity, but accessing any UI elements is not allowed from any thread other than the UI thread.

you can use runOnUiThread to fix it:

replace the lines that starts activity and stop the current with the following:

((Activity)getContext()).runOnUiThread(new Runnable() {

        @Override
        public void run() {
              Intent gameOver = new Intent(getContext(),GameOver.class);
              gameOver.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              getContext().startActivity(gameOver);
              ((Activity)getContext()).finish();
        }
 });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download