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从1.0到11版本特性
  2. Android初次使用遇到的两个有关问题
  3. [Android 新特性] Android 4.3新功能(正式发布前)
  4. Android Studio3.0新特性及安装详解
  5. Android Button按钮两个页面切换
  6. [Android各版本特性]Android 7.0 Nougat
  7. Android 开发时遇到的两个模拟器问题
  8. Android Lollipop新特性

随机推荐

  1. 移动端布局学习与 淘宝首页基本框架搭建
  2. Linux之export命令
  3. 移动端布局基本常识和仿淘宝手机站
  4. 常用函数类型和数据类型学习小结
  5. 【北亚服务器数据恢复】LUN映射出错导致
  6. 详解PHP中的命名空间
  7. 详解PHP中的数据库连接持久化
  8. PHP缓存系统APCu扩展的使用
  9. PHP中include和require的使用详解
  10. 详解iOS中跨页面状态同步方案比较