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.  执行效果和微信的效果一样。

 

你可能感兴趣的:(android,后台运行,返回键,最小化,moveTaskToBack)