简体   繁体   English

libgdx使android后退按钮的作用类似于主页按钮

[英]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) 我希望Android设备的后退按钮在按下时像主页按钮一样工作(暂停应用程序并失去焦点)

i have this : 我有这个 :

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

@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 ? 但是调用pause()方法是不够的,我该如何调用使其使其像主页按钮一样?

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 在Android模块中实现以上界面

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

Get reference of your implementation in core module and call implemented method on keyDown back button 获取核心模块中实现的参考,并在keyDown后退按钮上调用实现的方法

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

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

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

call YourActivity.this.moveTaskToBack(true); 调用YourActivity.this.moveTaskToBack(true); instead of pause(); 而不是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. 在您要操作后退按钮(例如主页按钮)的活动中添加以上代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM