Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage the back button with multiple scene

I followed this tutorial http://www.andengine.org/forums/tutorials/multiple-screen-andengine-game-v2-t4755.html to create a simple application with multiple scene and only one activity. I'd like to know how can i can return to the previous scene when i use the back button and finish the activity when i'm in the first scene.

I tried so in the MultiScreen Class:

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

replacing the core.getEngine().setScene(scene); in the SceneManager with this.mEngine.getScene().setChildScene(scene);


scene work differently from how I understood, I resolve with:

    @Override
    public void onBackPressed()
    {
        Scene scene = this.mEngine.getScene();
        if(scene.hasChildScene()){
            scene.back();
        }
        else{
            this.finish();
        }
    }
like image 657
Matteo Avatar asked Feb 09 '26 22:02

Matteo


1 Answers

You can override the back key in one of two ways, either by overriding the onBackPressed() method, or the dispatchKeyEvent() method

Overriding onBackPressed:

@Override
public void onBackPressed()
{
    // your code here
}

Overriding dispatchKeyEvent:

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        // your code here
    }
    return (yourbooleanhere);
}
like image 55
eplewis89 Avatar answered Feb 15 '26 13:02

eplewis89



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!