YVND
YVND

Reputation: 379

libgdx make android back button act like home button

I want the back button of an android device to act like the home button when pressed (pause the app and lose focus)

i have this :

(Gdx.input.setInputProcessor(this); and Gdx.input.setCatchBackKey(true); are called)

@Override
public boolean keyDown(int keycode) {
    if(keycode == Input.Keys.BACK) {
        pause();
    }
    return false;
}

but calling the pause() method isn't enough, what can i call to make it act like the home button ?

Upvotes: 0

Views: 138

Answers (2)

AAryan
AAryan

Reputation: 20140

Gdx.input.setCatchBackKey(true); // This will prevent the app from being paused.

Use Interfacing and call platform specific code by core module

Inside core module

interface PlatformService{

    fun moveToStack()
}

Implement above interface in Android module

@Override
public void moveToStack() {
    this.moveTaskToBack(true);
}

Get reference of your implementation in core module and call implemented method on keyDown back button

 Gdx.input.setInputProcessor(new InputAdapter(){
        @Override
        public boolean keyDown(int keycode) {

            if (keycode==Input.Keys.BACK){
                 platformService.moveToStack();
            }

            return super.keyDown(keycode);
        }
    });

Upvotes: 1

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8305

call YourActivity.this.moveTaskToBack(true); instead of pause(); method like:

  @Override
public void onBackPressed() {
    this.moveTaskToBack(true);
}

Add above code in activity where you want to act back button like home button.

Upvotes: 0

Related Questions