在返回按钮事件背景的移动应用程序应用程序、按钮、背景、事件

2023-09-07 17:54:13 作者:精神病院26床

是否有可能在移动后台应用程序时,我们的Andr​​oid点击设备后退按钮?

Is it possible to move the app in background when we click on device back button in android?

背景从那里启动意味着应用程序移动到屏幕(应用程序)?

background means application moves to screen(application) from where it launches ?

推荐答案

我将preFER使用低于code键移动在后台应用

I will prefer to use below code to move application in background

       @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        switch(keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
              if(isTaskRoot()) {
               Intent homeIntent = new Intent(Intent.ACTION_MAIN);
               homeIntent.addCategory(Intent.CATEGORY_HOME);
               homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               startActivity(homeIntent);
               return true;
                }
            else {
             super.keyDown(keyCode,event);
             return false;      
        }

    default: 
    super.keyDown(keyCode,event);
           return false;
 }

}