在现代版本的Android中是否会自动清除任务? [英] Does automatic task clearing occur in modern versions of Android?

查看:66
本文介绍了在现代版本的Android中是否会自动清除任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Android文档,系统将清除它认为已被用户放弃的任务(完成启动该任务的所有活动):

https://developer.android.com/guide/components/tasks-and-back-stack.html#Clearing

如果用户长时间离开任务,系统将清除除根活动以外的所有活动的任务.当用户再次返回任务时,仅还原根活动.系统以这种方式运行,因为经过很长一段时间后,用户可能已经放弃了以前的工作,而返回到任务以开始新的工作.

https://developer.android.com/guide/topics/manifest/activity-element.html#always

通常,在某些情况下,当用户从主屏幕重新选择该任务时,系统会清除任务(从堆栈中删除根活动上方的所有活动).通常,如果用户在一定时间(例如30分钟)内没有访问任务,则可以执行此操作.

可以在运行Gingerbread及更早版本的设备上轻松重现此行为.启动一个应用程序并创建一些历史记录,然后按主页"按钮并等待半小时.从主屏幕再次启动该应用,状态已清除,就好像它正在开始新任务一样.完美.

但是,在运行ICS或更高版本的设备上,即使任务在数小时或数天后处于非活动状态,我似乎也根本无法重现此行为.从主屏幕重新启动应用程序后,任务始终处于我留在其中的状态.

假设文档正确无误,现代版本的Android(API 14+)在什么条件下会自动清除任务?

如果行为已更改且文档已过时,< activity/> alwaysRetainTaskState 属性的用途是什么?默认值是否已更改为"true" 或此属性现在已弃用?

注意:我在这里不是在谈论Android的进程生命周期管理,这将取决于设备资源.无论如何,终止进程对用户都是透明的,并且不影响任务状态.

解决方案

好问题,经过一些消息来源的潜水后,答案肯定使我感到惊讶!

快速查看Android来源似乎可以找到答案.让我们从在ActivityStack.java ,但基本结构相同.这次,在第125行附近定义了常量:

 //当用户返回任务时,我们需要多久才能重置任务.目前//已禁用.静态最终长ACTIVITY_INACTIVE_RESET_TIME = 0; 

在1973行附近发现了相同的 resetTaskIfNeededLocked()方法,您可以看到现在它在应用相同的超时检查清除任务状态之前会检查该值是否大于零.但是请注意,此方法仍会检查 FLAG_ALWAYS_RETAIN_TASK_STATE ,因此此标志仍可用于保护状态清除,但似乎在禁用外部检查的情况下,该代码将永远不会执行.

总体而言,这似乎是令人信服的证据,表明该功能已在AOSP中针对更高版本的Android有效禁用.我看不到通过设备重新启用此值的外部手段(通过系统属性等),除非制造商使用此处添加的值来重建代码...但这并不常见.大多数ODM坚持使用XML的配置属性或它们可以通过覆盖控制的系统属性.

因此,从技术上讲,该功能尚未删除",但在我看来,该文档在延迟后自动触发方面已不再是正确的.

According to the Android documentation, the system will clear a task (finish all Activities above the one that launched the task) that it deems to have been abandoned by the user:

https://developer.android.com/guide/components/tasks-and-back-stack.html#Clearing

If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.

https://developer.android.com/guide/topics/manifest/activity-element.html#always

Normally, the system clears a task (removes all activities from the stack above the root activity) in certain situations when the user re-selects that task from the home screen. Typically, this is done if the user hasn't visited the task for a certain amount of time, such as 30 minutes.

This behaviour can be easily reproduced on devices running Gingerbread and earlier. Launch an app and create some back history, then hit the home button and wait half an hour. Launch the app again from the home screen and the state has been cleared as if it were starting a new task. Perfect.

However, on devices running ICS and above I cannot seem to reproduce this behaviour at all, even after a task has been inactive after many hours or days. When an app is relaunched from the home screen the task is always in the state I left it in.

Assuming the documentation is correct, under what conditions will modern versions of Android (API 14+) automatically clear a task?

If the behaviour has changed and the documentation is out of date, what is the purpose of the alwaysRetainTaskState attribute for <activity/>? Has the default value changed to "true" or is this attribute now deprecated?

Note: I am not talking here about Android's process lifecycle management, which will be device resource dependent. Killing a process should be transparent to the user anyway and does not affect the task state.

解决方案

Great question, after a bit of source diving the answer certainly surprised me!

A quick look at the Android sources seems to provide the answer. Let's start by looking back in Android 2.2 at ActivityManagerService.java. Notice around line 186 a constant defined called ACTIVITY_INACTIVE_RESET_TIME that happens to be set to 30 minutes.

// How long until we reset a task when the user returns to it.  Currently
// 30 minutes.
static final long ACTIVITY_INACTIVE_RESET_TIME = 1000*60*30;

Look a little further for the resetTaskIfNeededLocked() method around line 7021 and you will see this value checked to determine if the task should be reset before being launched.

Fast-forward to the Android 4.3 sources and the code has been moved into ActivityStack.java that is called from ActivityManagerService, but the basic structure is the same. This time, the constant is defined around line 125:

// How long until we reset a task when the user returns to it.  Currently
// disabled.
static final long ACTIVITY_INACTIVE_RESET_TIME = 0;

The same resetTaskIfNeededLocked() method is found around line 1973, and you can see that now it checks if the value is greater than zero before applying the same timeout check to clearing the task state. Notice, though, that this method does still check FLAG_ALWAYS_RETAIN_TASK_STATE, so this flag can still be used to protect a state clear, but it seems that with the outer check disabled this code will never be executed.

Overall, this seems like pretty compelling evidence that the feature has been effectively disabled in AOSP for later versions of Android. I do not see an external means (via system properties, etc.) for this value to be re-enabled per device unless the manufacturer were to rebuild the code with a value added here...but that is uncommon. Most ODMs stick to config properties in XML or system properties that they can control via an overlay.

So while technically the feature hasn't been "removed", it would seem to me that the documentation is no longer correct in terms of it auto-triggering after a delay.

这篇关于在现代版本的Android中是否会自动清除任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆