Java 我需要在单击后退按钮时最小化 android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28162815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:42:27  来源:igfitidea点击:

I need to minimize the android application on back button click

javaandroidandroid-activity

提问by

I need to minimize the application when back button is pressed.

按下后退按钮时,我需要最小化应用程序。

I use following code to catch hardware back button click event

我使用以下代码来捕获硬件后退按钮单击事件



help me with the code of minimize on back key pressed

帮我按下后退键的最小化代码

@Override
public boolean onKeyDown(int keyCode, keyEvent event) {
    switch(keyCode) {
    case KeyEvent.KEYCODE_BACK;
    //minimize application
    return true;
    }
    return super.onKeyDown(keyCode, event);
}

采纳答案by Kingfisher Phuoc

I think that you need to treat backevent as homeevent. The code below is how I emulate homepressed when User press backbutton:

我认为您需要将back事件视为home事件。下面的代码是我home在用户按下back按钮时模拟按下的方式:

 public void minimizeApp() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

回答by RajaReddy PolamReddy

try this code, this will minimize Activity.

试试这个代码,这将最小化活动。

public boolean onKeyDown(int keyCode, KeyEvent event)  
{
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
     {
        this.moveTaskToBack(true);
        return true;
     }
    return super.onKeyDown(keyCode, event);
}

or

或者

If you want to close the activity use this.finish()method to close the current running activity. instead of this.moveTaskToBack(true);

如果要关闭活动,请使用this.finish()方法关闭当前正在运行的活动。代替this.moveTaskToBack(true);

回答by DarckBlezzer

This is a simple code to minimize the application

这是一个最小化应用程序的简单代码

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