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

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

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

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

代码如下:

Manifest.xml

view plaincopy to clipboardprint?
<activity android:name=".OnNewIntentDemo"
android:launchMode="singleTask"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
</intent-filter>
</activity>
<activity android:name=".OnNewIntentDemo"
android:launchMode="singleTask"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
</intent-filter>
</activity>

Activity 中

view plaincopy to clipboardprint?
@Override
protected void onNewIntent(Intent intent) {
if(DEBUG) Log.i(TAG, "onNewIntent ~~~~~~~ intent = "+intent);
super.onNewIntent(intent);
}
@Override
protected void onNewIntent(Intent intent) {
if(DEBUG) Log.i(TAG, "onNewIntent ~~~~~~~ intent = "+intent);
super.onNewIntent(intent);
}

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

补充说明:

在研发的项目中由于使用到 播放器为单例 singleInstance,再次调用,需要使用onNewIntent

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. Material Design:利用RecyclerView CardView实现新闻卡片样式
  6. 对android LinearLayout中layout_weight属性使用
  7. Android(安卓)Studio使用说明
  8. Android(安卓)的Camera架构介绍
  9. Android(安卓)EditText/TextView使用SpannableString显示复合文

随机推荐

  1. Android的Activity的小知识点
  2. Android(安卓)ListView带CheckBox实现单
  3. android webview的 一些设置(js java交互
  4. Android动画Animator开发问题
  5. Android实现一款不错Banner界面广告图片
  6. android webview 显示不出来
  7. Android新控件MotionLayout介绍(一)
  8. Android敏感词标红
  9. Android(安卓)Studio 将module打成jar包
  10. Android官方培训中文课程