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
I need to minimize the android application on back button click
提问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 back
event as home
event. The code below is how I emulate home
pressed when User press back
button:
我认为您需要将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);
}