- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果中间没有 onDestroy,onCreate 会在 onStop 之后调用吗?
从下图来看,答案是肯定的。
但是在same page ,它说:
protected void onStop()
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
即使用户离开另一个应用程序或关闭屏幕,我的应用程序也需要运行后台任务。唯一应该停止任务的情况是应用程序完全关闭。所以我在 onCreate()
中创建并运行后台线程,并在 onDestroy()
中停止它。我面临的问题是有时我的多个线程同时运行。当我离开我的应用程序很长时间然后又回来时,经常会发生这种情况。我认为这可能是由于 onCreate()
的多次调用所致。如何解决?
最佳答案
Will onCreate called after onStop without onDestroy in the middle?
简短回答:否
长答案:
如果在activity
上仅调用了
onStop
,则不会调用
onCreate
。在 onStop
之后,如果用户导航回到 Activity,则会调用 onStart
(在此之前 onRestart
也将被调用)。
现在让我解释一下如何在 onStop
之后调用 onCreate
而没有调用 onDestroy
。
为此,您需要查看 android 中的这个进程优先级层次结构:
从 onResume
被调用但 onPause
未被调用时,Activity 保留在前台进程中。由于前台进程具有最高优先级,因此它不会被杀死。
onPause
在用户不再能够与 activity
交互但可见 时调用。示例:当您在 Activity 上方打开 DialogFragment
时。然后 Activity 过程进入可见过程。这比前台进程的优先级低,但有足够的优先级不会被杀死。
之后是onStop
,当用户不再能够看到 Activity 时调用,这意味着 Activity 不可见。这就是 Activity 流程进入后台流程的时候。现在,如果系统需要更多内存并且您的 Activity 比其他 Activity 消耗更多内存,它可以被杀死(这是本答案的题外话,哪个背景首先被杀死)。
因此,如果一个 Activity 在后台并被系统杀死,则不能保证调用 onDestroy
。在这种情况下,onCreate
将在 onStop
之后调用,而 onDestroy
不会被调用。
这就是为什么在问题图片( Activity 生命周期)中显示如果 App 进程被终止,onCreate
将在 onStop
之后调用.
编辑 1:
如果您想在 Activity 被销毁之前处理任何事情(如释放内存、终止内存密集型任务等),并且由于不能保证调用 onDestroy
,您可以实现 ComponentCallbacks2
(仅适用于 api 级别 >= 14)接收 onTrimMemory
回调与您的应用程序/Activity 的不同阶段。不同的阶段及其解释是 here .
如果您想在 API 级别 < 14 中实现相同的功能,您可以使用 onLowMemory
大致相当于 TRIM_MEMORY_COMPLETE
的回调.
关于android - 将在 onStop 之后调用 onCreate 而中间没有 onDestroy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36589046/
有人可以给我一个更简单的以下代码的解决方案(它正在展开给定结构 0xFC :: len :: payload :: ... :: 0x0A :: 0x0D 的整数列表): object Payload
我已经在我的网站上安装了 SSL 证书,但 intermediate.crt 无法正常工作。任何 SSL 检查器(例如 GeoTrust Checker)都告诉我,缺少中间 key 。网站上已经使用了
如何让图像从这个框的中间开始? (中间纵横) 最佳答案 有几种方法可以做到这一点,如果它需要在所有浏览器(IE7+ 和其他浏览器)中工作,你需要做不同的事情来让它在某些情况下工作。 使用绝对位置
如何强制 min-height 和 vertical-align:middle 为 td 元素或其内部元素工作? 最佳答案 td 元素上的 height 等同于 min-height,因为如果需要,表
我正在尝试自动滚动到订单簿的中间行。 我有 orderBook div,其中放置了带有 orderBook 的表。该表的其中一行有一个 id middleRow。我想做的是滚动该行并将其放置在 ord
我正在尝试在 javascript 中计算绝对定位元素的 transform-origin 属性,以便它们在悬停时填充整个视口(viewport)。 我尝试通过 x 除以窗口宽度和 y 除以窗口高度来
我有休闲字符串 ' this is my string ' 是否可以删除开头和结尾的所有空格,只在单词之间留一个空格。 要选择我使用过的所有空间: SELECT regexp_replace('
我正在设法创建我的第一个复杂的 J2E 解决方案,并且在每个教程中我都发现了某种中间表的用法,如下所示: 表:用户、用户角色、角色虽然逻辑会简单地向用户表添加一个键来引用它在角色表上的角色,但为什么要
我正在寻找以下解决方案。我想定位一个图像元素,例如 在中间。所以高度是视口(viewport)的高度,宽度会自动设置,图像的中间应该在视口(viewport)宽度的中间。 我搜索的一个例子就像下面的网
我正在设计一种布局,它更像是注册用户的个人仪表板。我让它变得简单,使用基本的 2 列网格,一个用于侧边栏,一个用于主要内容。 因为,例如,80% 的网站使用将发生在一个单独的子系统中,在无 chrom
我有三个不同的 div 标签(不在彼此内部)和代码,所以它有一个把单词放在左边、中间或右边,但中心非常偏离中心。这是 HTML 代码: .desc { float: right; color:
我有以下CSS http://jsbin.com/azivip/75/edit我想让黄色的 div 高度填充蓝色和绿色 div 之间的空间。使用高度继承似乎使 div 超出了绿色 div。 有什么想法
我不得不在其父元素的中间放置一些文本。我用下面的代码实现了它: #div1 { position: relative; margin: 0; padding: 0; } #div2 {
发现一个使用合法证书(由thawte 签名)的网站,但所有浏览器都会拒绝它。我不明白为什么。thawte 的支持告诉我一个域有两个证书,然后将这个 https://www.sslshopper[dot
我正在尝试使用 OpenSSL 创建证书链,但出于某种原因,当我在我的计算机上安装我的根 CA 并尝试验证证书链时,它总是告诉我它找不到证书的颁发者.为了让事情发生,我必须安装中间 CA,这是没有意义
我看到 REST 的一大好处是依赖 HTTP 缓存。我不是在争论这个,而是完全认同这个想法。但是,我从来没有看到对中间 HTTP 缓存的更深入的解释。 如果我将 Cache-control heade
查看此图片 Facebook Messenger Android App Buttons ( MESSENGER\ACTIVE ) 我怎样才能做到这一点? 详细信息:- 带有 2px 红色边框的 di
我的任务是制作漂亮的文本,在文本中间加一条白线,如下图所示。是否可以使用 css 来实现?这是 Fiddle .container{ height:200px; width:400px;
在拉丁文字中,字母有大写和小写形式。在 Python 中,如果你想比较两个字符串而不考虑它们的大小写,你可以使用 'string'.upper() 或 'string'.lower() 将它们转换为相
我正在使用 awk 对文件进行一些文本处理。例如删除尾随空格。 awk '{gsub(/ +$/, "")} {print $0}' filename 这很好用。但是当我将输出重定向到原始文件时。它变
我是一名优秀的程序员,十分优秀!