Android 应用退到后台
2016-04-21 22:36
369 查看
Android 应用退到后台
2016-4-21 10:29:26Android L
moveTaskToBack(boolean nonRoot)
把包含这个Activity的任务转到后台。并不是finish。传入Boolean参数:如果是false,在这个Activity是任务的根Activity时,方法才会起效。
传入true,任务中任意Activity都会起效。
/** * Move the task containing this activity to the back of the activity * stack. The activity's order within the task is unchanged. * * @param 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. * * @return If the task was moved (or it was already at the * back) true is returned, else false. */ public boolean moveTaskToBack(boolean nonRoot) { try { return ActivityManagerNative.getDefault().moveActivityTaskToBack( mToken, nonRoot); } catch (RemoteException e) { // Empty } return false; }
我们可以监听菜单键,按菜单键把APP退到后台:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { moveTaskToBack(true);// 点击菜单键即转入后台,vivo X6Plus Android5.1也适用 return true; } }
点击返回键,退出当前Activity
点击返回键,执行onBackPressed(),最后会调用
finish()。但是进程并没有被杀死。
Activity会进入onPause()。在合适的时机,Activity会进入onResume(),恢复状态。
/** * Take care of popping the fragment back stack or finishing the activity * as appropriate. */ public void onBackPressed() { if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) { supportFinishAfterTransition(); } }
/** * Reverses the Activity Scene entry Transition and triggers the calling Activity * to reverse its exit Transition. When the exit Transition completes, * {@link #finish()} is called. If no entry Transition was used, finish() is called * immediately and the Activity exit Transition is run. * * <p>On Android 4.4 or lower, this method only finishes the Activity with no * special exit transition.</p> */ public void supportFinishAfterTransition() { ActivityCompat.finishAfterTransition(this); }
/** * Reverses the Activity Scene entry Transition and triggers the calling Activity * to reverse its exit Transition. When the exit Transition completes, * {@link Activity#finish()} is called. If no entry Transition was used, finish() is called * immediately and the Activity exit Transition is run. * * <p>On Android 4.4 or lower, this method only finishes the Activity with no * special exit transition.</p> */ public static void finishAfterTransition(Activity activity) { if (Build.VERSION.SDK_INT >= 21) { ActivityCompat21.finishAfterTransition(activity); } else { activity.finish(); } }
相关文章推荐
- Android自定义控件:仿美团下拉菜单及相关代码优化
- Android属性动画实现TextView类似支付宝余额数字滚动
- android开发中常用的开源框架与好的项目
- Android Scroll分析
- android设计模式的使用之适配器模式
- Android简单的调用系统相机和相册
- 命令行创建Android应用,命令行生成签名文件,命令行查看签名信息,对APK包签名并编译运行
- Android ADB 端口占用问题解决方案
- Android 学习笔记之一 Status Bar Notifications
- Android Scroll详解(三):Android 绘制过程详解
- android tv开发adb 命令
- android 更加复杂的小鱼游
- android 小鱼在水中游
- Android实现图片自动轮播并且支持手势左右无限滑动
- Fragment使用方法总结
- 最详细的 Android Toolbar 开发实践总结
- Android事件分发机制
- Android学习笔记--Intent
- Java(Android)线程池
- Android 短信箱操作