最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验:

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

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

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

代码如下:

Manifest.xml

<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 中

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


注意: 当按Home键退出,再长按Home键进入,此时onNewIntent不被访问,因为再次进入的时候没有被发起Intent。
来自:http://blog.csdn.net/adreamer_bj/archive/2010/12/22/6091414.aspx

更多相关文章

  1. linux下eclipse启动android模拟器出现PANIC: Could not open: /h
  2. 转 android launch flow
  3. 如何启动Android(安卓)SDK 1.5模拟器
  4. Android(安卓)启动Activity面流程(Android(安卓)9.0)
  5. android启动--深入理解zygote
  6. Activity的四种启动模式和onNewIntent()
  7. flutter包名,应用名称,图标,启动图片修改
  8. Android(安卓)彻底退出自己APP 并杀掉所有相关的进程
  9. Android(安卓)开机启动流程分析

随机推荐

  1. Android(安卓)Studio下载及离线升级方法
  2. Android中的5大布局和网格布局
  3. Android的控件属性
  4. Android中TextView内容过长加省略号
  5. Android(安卓)TextView内容过长加省略号
  6. android:inputType标签
  7. 【Android(安卓)UI设计与开发】之具体解
  8. Android(安卓)RelativeLayout属性介绍
  9. 安装android的sdk
  10. Android(二)HelloWorld,Android(上)