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. 我使用过的 控件的一些特性(layout_weight,paddingleft)
  2. android 5.0新特性
  3. Android M5 新特性
  4. 2012版辅助开发工具包(ADT)新功能特性介绍及安装使用
  5. android和ios的系统特性区别
  6. Android Studio3.0新特性及安装图文教程
  7. android 5.0 新特性
  8. android studio 3.6.0 绑定视图新特性
  9. Android开发之5.0特性深入理解(一)

随机推荐

  1. Android(安卓)APP使用自定义字体实现方法
  2. Android使用Dialog风格弹出框的Activity
  3. Android的线程使用来更新UI----Thread、H
  4. android解析json(2)
  5. Android面经| 问题归纳
  6. inputType属性
  7. Android,采用AIDL编写接口实现IPC 入门
  8. android apk获得系统权限
  9. Android属性动画之XML定义方式
  10. android TextView的字体颜色设置的多种方