android 按返回键最小化(后台运行)

在网上搜,发现有部分人提出的是如下代码:

 //   Intent intent = new Intent();
//   intent.setAction("android.intent.action.MAIN");
//   intent.addCategory("android.intent.category.HOME");
//   startActivity(intent);

      此代码实际是跳转到系统桌面。实际只需下述关键一行代码

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

Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.

  • Parameters:

  • nonRoot If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.

  • Returns:

  • If the task was moved (or it was already at the back) true is returned, else false.

分析:nonRoot“非根”,如果参数为true 则“是 -非根”,也就是如果当前界面不是应用程序的根界面;如果是false,则

“非-非根”,双重否定为肯定,“是根”,也就是如果当前界面是应用程序的根界面。上述示例代码中,当前界面并不是应用程序的根界面,所以传入的是true.  执行效果和微信的效果一样。

 

转载于:https://my.oschina.net/jixin/blog/374280

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值