3

I can't correctly exit from PlayN GameActivity using Back button.

I am using following code:

public class Loader extends GameActivity {
    public void main(){
        PlayN.run(new Game());  
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}

but it is not correct enough.

GameActivity closes, but if you run game again, it will shows two copies of the same game with two working threads (previous and current) and in root layer you will see two copies of root layers.

I'ts really stupid, but how to do finish() correctly to destroy GameActivity with all his internal threads/listeners/layers/audio completely from mem?


this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        this.finish();
    }
    return super.onKeyDown(keyCode, event);
}

works only if you didn't used PlayN.assets().getSound(...).play() in your game, otherwise all Activity crashes on exit.

I found a small workaround for this, you should override onDestroy method in your GameActivity by this:

@Override
public void onDestroy() {
    super.onStop();
    try {
        super.onDestroy();
    } catch (IllegalStateException e) {
        android.util.Log.e("Easy", "JVM: playn.android.AndroidCompressedSound.MediaPlayer melts my brain!.");
    }
}

but still have no idea why this exception happens.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154

1 Answers1

0

First, try to use: this.finish(); instead of finish();

OR use the below technique:

Override the onDestroy of the current activity like this:

  @Override
protected void onDestroy() {
    super.onDestroy();
    System.runFinalizersOnExit(true);
    System.exit(0);

}

and call it, i.e this.onDestroy();

Imp: It'll kill all the ongoing processes related to ur app!

Mohit
  • 2,940
  • 2
  • 24
  • 32
  • What would be the difference between `finish()` and `this.finish()`? – nhaarman Aug 26 '12 at 17:30
  • This is how i usually call, so i just suggested my way. I know calling finish and this.finish is not going to make any difference in this particular case. Both are correct and i don't think there is need to down vote this. – Mohit Aug 26 '12 at 18:26