android:launchMode="singleTask"和 onNewIntent(Intent intent)两个特性,现总结一下经验:

android:launchMode="singleTask" 配置在 Mainifest 中,它保证了栈中此Activity总是只有一个,无论你启动它多少次;

onNewIntent(Intent intent) 是Override Activity的父类方法,只有仅在点Home键退出Activity而再次启动新的Intent进来才被调用到;

它们两结合使用,可以做到监听home键(仅当发起新的Intent)。

代码如下:

Manifest.xml

  1. < activity android:name = ".OnNewIntentDemo"
  2. android:launchMode = "singleTask"
  3. android:label = "@string/app_name" >
  4. < intent-filter >
  5. < action android:name = "android.intent.action.MAIN" />
  6. < category android:name = "android.intent.category.LAUNCHER" />
  7. </ intent-filter >
  8. < intent-filter >
  9. < action android:name = "android.intent.action.VIEW" />
  10. < category android:name = "android.intent.category.DEFAULT" />
  11. < data android:mimeType = "video/*" />
  12. </ intent-filter >
  13. </ activity >

Activity 中

  1. @Override
  2. protected void onNewIntent(Intentintent){
  3. if (DEBUG)Log.i(TAG, "onNewIntent~~~~~~~intent=" +intent);
  4. super .onNewIntent(intent);
  5. }

注意: 当按Home键退出,再长按Home键进入,此时onNewIntent不被访问,因为再次进入的时候没有被发起Intent。

更多相关文章

  1. android开机启动的问题,android:installLocation
  2. Activity的四种启动模式和onNewIntent()
  3. Activity的四种启动模式和onNewIntent()
  4. 启动AVD时候失败PANIC: ANDROID_SDK_HOME is defined but could
  5. Android(安卓)保活后台启动Service 8.0踩坑记录
  6. Android脚本环境
  7. Android的Activity启动流程分析
  8. android 广播 android Service 开机启动的Service
  9. Android(安卓)返回键退出

随机推荐

  1. Matisse Android(安卓)图片选择器
  2. Ubuntu 14.04 连接Android 手机(三星note
  3. Android添加半透明操作提示的方法
  4. Android学习--使用url访问网络资源
  5. android 9.0 获取U盘路径
  6. android之ViewFlipper滑屏切换效果
  7. 获取视频缩略图
  8. Android -- 状态栏高度
  9. 2012-7-20 android 图片叠加效果——两种
  10. android缺陷分析:内核空指针