android 应用移至后台时是否会清除视图中的数据?

Does android clear data in Views when app moves to background?

我的应用程序有以下安全要求:

验证应用在后台运行时是否从视图中删除了敏感数据。

我的问题是,Android 是否在后台时从视图中删除数据?

根据 android documentation 我知道的是:

默认情况下,系统使用 Bundle 实例状态来保存有关 activity 布局中每个 View 对象的信息(例如输入到 EditText 小部件中的文本值)。

一个解决方案是我可以清除所有视图然后使用以下方法保存状态

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
    } else {
        // Probably initialize members with default values for a new instance
    }
    // ...
}

但我不知道 Android os 是否清除了 views.Any 中的数据,将不胜感激。

谢谢

Android 不会 在您的应用程序移至后台后立即自动回收您的视图。

The system kills processes when it needs to free up RAM; the likelihood of the system killing a given process depends on the state of the process at the time.

如果它决定终止视图或需要内存,它只会回收视图。或者在配置更改时。

A user expects an activity’s UI state to remain the same throughout a configuration change, such as rotation or switching into multi-window mode. However, by default the system destroys the activity when such a configuration change occurs, wiping away any UI state stored in the activity instance.

总的来说,我认为你的方法是正确的。

如果您愿意,只需使用 onStop()(而不是 onPause(),因为某些对话框和其他交互会从您的 Activity 中移除焦点)来清除您的视图。

看看 android activity 生命周期 https://developer.android.com/guide/components/activities/activity-lifecycle

确保清除所有数据的一种方法是覆盖 onPause 函数并手动清除数据。当您的应用程序移至后台时调用 onPause。

不,它不会删除数据,因为根据 Android 的生命周期,当您为视图设置背景时,将调用 onPause() 和 onResume() 并且您的视图及其实例位于 onCreate 中() 方法。

因此,通过从后台启动视图,它不会删除您的视图数据。