关于onNewIntent的使用

2023-05-16

摘自:http://blog.sina.com.cn/s/blog_5da93c8f0101hnzx.html

当从栈中启动一个已经存在的Activity时,系统不会再执行onCreate方法,而是执行onNewIntent方法。

总结:

(1)Activity第一启动的时候执行onCreate()---->onStart()---->onResume()等后续生命周期函数,也就时说第一次启动Activity并不会执行到onNewIntent().

(2) 而后面如果再有想启动Activity的时候,那就是执行onNewIntent()---->onResart()------>onStart()----->onResume().

(3)如果android系统由于内存不足把已存在Activity释放掉了,那么再次调用的时候会重新启动Activity即执行onCreate()---->onStart()---->onResume()等。

当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIntent()都是得到老的Intent。

介绍两个关于onNewIntent()的使用实例

实例1:Android 监听home键(android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法

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);
}

实例2:发通知 PendingIntent 中Intent 内容没有更新

Xml代码

android:launchMode="singleTask" />
当我们把Activity 启动模式设置为 singleTask 之后 当我们下次 再去 用Intent 启动 这个 Activity 的时候 就不会去调用 onCreate方法 而是去调用onNewIntent()方法 然后把Intent中的数据传给它 , 前几天遇到的问题是 当我 发一个通知给状态栏 然后点击这个通知 自然会执行 PendingIntent 里边的Intent。 但是 在Activity那边的 onNewIntent()方法里边 得到的数据 不是最新的 也就是说 是 第一次的 以后 不管我怎么点通知 它都 是 第一次点击通知得到的数据,当以后再点击通知的时候其实 数据已经变了 但是 onNewIntent()方法总是得不到最新的数据, 无语了很久, 去 农民伯伯翻译组 发问得解 需要给 PendingIntent 加一个 FLAG

Java代码
PendingIntent contentIntentBegin = PendingIntent.getActivity(
notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);


最后一个参数就是 FLAG,这个FLAG 的 意思就是:如果系统中已存在该PendingIntent对象,那么系统将保留该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。这个非常有用,例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据,达到在不同时机传递给MainActivity不同的参数,实现不同的效果。

就是 Intent里边的数据没更新而已, 很2个问题 搞了很久 才发现原来加个FLAG 就行了,有点伤不起了。!!

代码片段
Java代码
public void showNotiMessageBegin(String message, int requestCode,
String itemid) {
notification = new Notification(R.drawable.skyfile_upload_noti,
message, System.currentTimeMillis());
if (requestCode == 1 && notification.contentIntent == null) {
int index = itemid.lastIndexOf("/");
final String backPath1 = itemid
.substring(0, index == 0 ? 1 : index);

Intent inStart = new Intent(notificationContext, SkyfileActivity.class);
inStart.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
inStart.setData(Uri.parse(MetaDataView.class.getName()));
inStart.putExtra(MetaDataView.BUNDLE_PATH, backPath1);
PendingIntent contentIntentBegin = PendingIntent.getActivity(
notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = contentIntentBegin;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(notificationContext,
UPLOATTITLE, message, contentIntentBegin);
notificationMgr.notify(1, notification);
} else {
notification.contentIntent.cancel();
}
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于onNewIntent的使用 的相关文章

  • Activity onNewIntent详解

    onNewIntent 的触发时间 xff1a onNewIntent png 如图所示 xff0c onCreate 和 onNewIntent 不会被同时调用 官方文档 xff1a onNewIntent added in API le
  • 每天进步一点点之Android基础(3)—— Activity的onNewIntent

    onNewIntent 的触发时间 xff1a 如图所示 xff0c onCreate 和 onNewIntent 不会被同时调用 如果在 AndroidManifest xml 中 xff0c 将 Activity 的 launchMod
  • onNewIntent()的使用

    我对 onNewIntent 的理解 当我们由于某些原因 xff0c 可能会反复启动一个 Activity 时 xff0c 你可能会想不就是通过 startActivity intent xff0c 来启动嘛 xff0c 反复走 onCre
  • [android nfc] onNewIntent 中 无法获取 tag, 或者 intent.extras 为空, 或者 intent.action 为空

    在做android nfc 场景的时候 大方向上有2种 foreground 调度系统enableReaderMode api foreground 调度系统 发现android12下无法获取tag信息 经过排查 原因如下 在 androi
  • android的onNewIntent

    1 onNewIntent Intent intent 是Activity类的方法 它被调用发几种情况如下 lt activity android name 61 34 NewIntentDemo 34 android label 61 3
  • onNewIntent使用遇到的坑

    onCreate是用来创建一个Activity也就是创建一个窗体 xff0c 但一个Activty处于任务栈的顶端 xff0c 若再次调用startActivity去创建它 xff0c 则不会再次创建 若你想利用已有的Acivity去处理别
  • Activity onNewIntent注意事项

    数据上报发现 xff0c onNewIntent 以后 xff0c onResume和onPause可能不会执行 xff0c 直接执行onStop
  • Android:onNewIntent()触发机制及注意事项

    为什么80 的码农都做不了架构师 xff1f gt gt gt 一 onNewIntent 在 IntentActivity 中重写下列方法 xff1a onCreate onStart onRestart onResume onPause
  • Activity onNewIntent方法的调用时机

    首先看下官方的API说明 xff1a This is called for activities that set launchMode to singleTop in their package or if a client used t
  • [Android] 以singleInstance模式加载的Activity怎么接收以Bundle方式传递过来的参数 By onNewIntent() but not onResum

    问题来自这儿 xff0c Bundle在接收时未更新 xff0c http blog csdn net dadoneo article details 8164058 虽然可以暂时解决问题 xff0c 但并未说到根本原因 xff0c 下面就
  • android:onNewIntent

    本文编写目的 xff1a 供自己查阅 1 通过Intent启到一个Activity的时候 xff0c 就算已经存在一个相同的正在运行的Activity 系统也会创建一个新的Activity实例 为了不让Activity实例化多次 xff0c
  • Activity的onNewIntent

    一个应用的Activity可供多种方式调用启动 xff0c 当多个调用希望只有一个Activity的实例存在 xff0c 并且还要区分是被谁启动或是已经启动被谁拉到前台来的 xff0c 这就需要Activity的onNewIntent In
  • 关于onNewIntent()

    普通的Activity之间的跳转 xff0c 如 xff1a 新打开一个Activity xff0c 此时的执行顺序是0nCreat xff0d onStart xff0d onResume 此时使用Intent 传递数据没有问题 xff0
  • 关于onNewIntent的使用

    摘自 http blog sina com cn s blog 5da93c8f0101hnzx html 当从栈中启动一个已经存在的Activity时 xff0c 系统不会再执行onCreate方法 xff0c 而是执行onNewInte
  • 关于onNewIntent理解

    首先介绍一下Android的四种启动模式 standard 默认的 xff1a 所有的activity实例放在一个task xff08 任务栈 xff09 中 xff0c 遵循先进后出 xff0c 后进先出的规则 singleTop xff
  • Activity中使用onNewIntent方法避免多次实例化同一个Activity

    最近写的项目中有一个搜索 搜索结果 搜索这样一个循环的过程 xff0c 发现了几个问题 xff1a 1 循环导致多次实例化这两个类 xff1b 解决方案 xff1a 在Manifest里面对应activity下面设置启动模式为singleT
  • android onNewIntent 调用时机

    当前Activity已经在Activity堆栈当中时 xff0c 主要取决于LaunchMode对应的设置 LaunchMode为SingleTop时 xff0c 如果ActivityA在栈顶 且现在要再启动ActivityA xff0c
  • Android Activity onNewIntent() 详解

    阅读更多 阅读难度 xff1a 中 阅读前提 xff1a 1 需要了解 Android 的生命周期 xff0c 每个方法的触发时机以及作用 2 需要了解 Activity 的 launchMode 模式和作用 3 Intent 基本知识及作
  • 关于onNewIntent你应该知道的

    一 API描述如下 大概意思是当Activity被设以singleTop模式启动 xff0c 当需要再次响应此Activity启动需求时 xff0c 会复用栈顶的已有Activity xff0c 还会调用onNewIntent方法 并且 x
  • onNewIntent() 在某些设备中没有被调用

    我正在为 Android 应用程序实现 Oauth twitter google 一些用户抱怨因为他们无法登录 分析问题后 我发现在某些设备中有时不调用 onNewIntent 而是调用 onCreate 方法 所以看来活动的任务 实例有问

随机推荐

  • 架构师成长之路工具篇(1):markdown撰写文档

    今天笔者想说的工具就是markdown xff0c 正所谓工欲善其事必先利其器 xff0c 选择高效的工具自然能提升工作效率 笔者使用的markdown工具是 xff1a typora word太重 xff0c 太复杂 xff0c 在写文档
  • 如何优雅的退出qemu虚拟环境

    在console环境下 xff0c 先 按 ctrl 43 a xff0c 释放之后再按 x 键 既可terminate qemu 注 xff1a 1 a 和 x 均为小写 2 必须先释放ctrl 43 a 之后 再按x键
  • xmake经验总结1:解决c++ future/promise抛出std::system_error的问题

    1 背景 1 1 场景 编译器 xff1a gcc 9 4 运行系统 xff1a Ubuntu 20 04 4 LTS xmake v2 6 7 场景 xff1a 其大致场景是使用c 43 43 的future promise功能 xff0
  • 神经网络实现手写数字识别(MNIST)

    一 缘起 原本想沿着 传统递归算法实现迷宫游戏 gt 遗传算法实现迷宫游戏 gt 神经网络实现迷宫游戏的思路 xff0c 在本篇当中也写如何使用神经网络实现迷宫的 xff0c 但是研究了一下 xff0c 感觉有些麻烦不太好弄 xff0c 所
  • 【AI】Auto-GPT:当时代抛弃你的时候,不会和你说一声再见!

    当时代抛弃你的时候 xff0c 不会和你说一声再见 xff01 Auto GPT Code Auto GPT 一个全自动化的GPT 4 Collecting BOTAI Auto GPT 官网网站
  • 解决visio对象在word中显示不全的问题

    作为一个软件工程师 xff0c 编写技术文档是常有的事情 xff0c 使用visio绘制各种图形 如 xff0c 流程图 xff0c 结构图 xff0c 框架图 xff0c 状态图等等 也是再正常不过的事情 如果我们在word中撰写文档时
  • git submodule使用以及注意事项

    一 背景 在平时的软件开发过程中常常会有这样的场景 xff0c 自己负责的某个模块会依赖其他模块或者第三方的library 这时你自己的模块是一个独立的代码仓库 xff0c 你想要实现这样一种功能 xff0c 当你从你的模块的代码仓库里把代
  • Webpack5 - 基本使用

    一 webpack有何作用 webpack是一个Javascript应用程序的模块打包器 它可以递归地构建一个应用程序的模块依赖关系图 xff0c 然后将所有模块打包在一起 为什么需要模块打包器 xff1a 现在的应用程序模块文件很多 xf
  • Vue.js - VueRouter的Hash与History模式 / 手写VueRouter

    一 Hash与History模式 Hash模式History模式url地址外观http localhost 8081 abouthttp localhost 8080 about原理基于锚点 xff0c 监听锚点变化时触发的onhashch
  • Vue.js - Vue.js响应式原理(1/2)

    一 数据驱动 数据响应式 xff1a 数据改变 xff0c 则视图改变 xff0c 避免频繁的Dom操作 xff0c 提高运行效率 双向绑定 xff1a 数据改变 xff0c 则视图改变 xff1b 视图改变 xff0c 则数据也随之改变
  • Vue.js - 模拟Vue.js响应式原理(2/2)

    项目仓库 xff1a https gitee com big right right vue responsive tree master L8 一 类的说明 Vue类 xff1a 保存传入的选项数据 xff0c 把选项data中的成员注入
  • .NET用NCO连接SAP RFC---写数据到SAP

    1 环境 xff1a a win7 43 64位操作系统 b VS2012 c nco3 0 xff08 64bit 下载网址 xff1a http www dllbang com dll sapnco dll xff09 xff0c d
  • .NET Framwork,C#入门开发教程,零基础必看

    初识 NET Framwork和开发过程 一 什么是 NET Framework NETFramework是一个开发平台 xff0c 可以在其上使用多种语言开发程序 xff1a 如C xff0c VB xff0c C 43 43 xff08
  • 如何清除IIS缓存

    问题描述 xff1a 在IIS的默认网站下创建了一个虚拟目录名A xff0c 然后删除这个虚拟目录 后来需要重新创建一个同名虚拟目录 xff0c 映射的还是原来的项目文件 xff0c 只是项目文件的物理路径发生了变化 xff0c 这个新虚拟
  • jqGrid 多选复选框 编辑列

    1 首先看一下效果 2 html代码 lt table id 61 34 grid table 34 gt lt table gt 3 在 function 方法中 xff0c 写如下方法 xff0c 用json数据填充jqGrid xff
  • 【NERF】及其变种

    CVPR 2023 Interactive Segmentation of Radiance Fields Nerf重建加上选中分割 Nerf 调研
  • Asp.net WebApi 项目示例(增删改查)

    1 WebApi是什么 ASP NET Web API 是一种框架 xff0c 用于轻松构建可以由多种客户端 xff08 包括浏览器和移动设备 xff09 访问的 HTTP 服务 ASP NET Web API 是一种用于在 NET Fra
  • C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法

    1 C 中的每一个委托都内置了BeginInvoke和EndInvoke方法 xff0c 如果委托的方法列表里只有一个方法 xff0c 那么这个方法就可以异步执行 xff08 不在当前线程里执行 xff0c 另开辟一个线程执行 xff09
  • Vue2 模板template的四种写法

    lt div id 61 34 app 34 gt lt h1 gt 我是直接写在构造器里的模板1 lt h1 gt lt div gt lt template id 61 34 demo3 34 gt lt h1 style 61 34
  • 关于onNewIntent的使用

    摘自 http blog sina com cn s blog 5da93c8f0101hnzx html 当从栈中启动一个已经存在的Activity时 xff0c 系统不会再执行onCreate方法 xff0c 而是执行onNewInte