3

When user press home button onStop() method is called and system takes screenshot which can be seen among open applications when user press and hold home button(on android phones). My question is how to prevent sensitive data to be visible to anybody who can take a phone and press and hold home button? Imagine, application(activity) presents a preview of secret document and user press home button(maybe somebody just approached to the user and he doesn't want to anybody see his secret document). However, anybody who takes a phone after that can press and hold home button and see some sensitive data.

I tried to hide view onPause and/or onStop but that doesn't work. So, how to remove current visible view before system takes screenshot after onStop?

Thanks.

xezo
  • 293
  • 3
  • 6

1 Answers1

3

My question is how to prevent sensitive data to be visible to anybody who can take a phone and press and hold home button?

Quoting myself:

However, for the user’s benefit, there may be reasons to block screenshots from certain of your activities. To do that, use FLAG_SECURE:

public class FlagSecureTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(LayoutParams.FLAG_SECURE,
                         LayoutParams.FLAG_SECURE);

    setContentView(R.layout.main);
  }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried, solution is good and nothing is shown when user press and hold home button but on application resume still previous view is displayed for a fraction of second even that view was hidden onStop. Any idea how to prevent that? – xezo Aug 03 '14 at 18:25
  • @xezo: I am sorry, but I have not seen the behavior that you are describing, and so I have no idea how to address it. – CommonsWare Aug 03 '14 at 18:41
  • FLAG_SECURE will also prevent screenshots from being taken, which is not always desirable. – rrbrambley Jan 30 '18 at 01:20