5

I want to hide my views when my app is backgrounded. For e.g. I want to hide this "Live Debug Mode" textview when my app is backgrounded.

Consider this backgrounded app for e.g.

Omkar Amberkar
  • 1,952
  • 4
  • 16
  • 21

2 Answers2

7

Add the following code to your onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);
    setContentView(R.layout.activity_main2);
    //...rest of your code
}
Bill
  • 4,506
  • 2
  • 18
  • 29
0

Edit: This answer does not work. I thought about deleting the answer, but then thought it would better serve as information to anyone searching this in the future. I am not sure why it doesn't work. Intuitively to me, it should.


Use the onPause lifecycle method to set the views you need hidden to either invisible or gone using setVisibility()

@Override
protected void onPause() {
    TextView tv = (TextView)findViewById(R.id.mytextview);
    tv.setVisibility(View.INVISIBLE);
    super.onPause();
}
MrSalmon
  • 89
  • 1
  • 5