Search code examples
androidandroid-activityviewandroid-lifecycle

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


I have the following security requirement for my app :

Verify that the app removes sensitive data from views when backgrounded.

My question is, Does Android remove data from views when backgrounded ?

What i know as per android documentation is that :

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText widget).

One solution is that i can clear all views then use below approach to save state

@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
    }
    // ...
}

But i have no idea of whether Android os clears data in views.Any help will be appreciated.

Thanks


Solution

  • Android does not automatically recycler your view as soon as your application moves to background.

    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.

    It just recycle views if it decided to kill them or if it's in need for memory. Or upon a configuration change.

    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.

    Overall I think you had the right approach.

    And just use onStop() (and not onPause() because of some Dialogs and other intercations that removes focus from your Activity) to clear your views if you want to.