Here is a solution that allows you to hide the contents of the application by closing it with a splash screen when the application is placed in the background. This does not use the FLAG_SECURE technique, I just redefine the onPause and onResume methods of the screens and change the view to show the one that covers everything in the back.
First, I create the splash screen in a separate file in the form of relative markup with the name splash_screen_custom, I also assign the relative markup in the id customSplash file. Pay attention to the height adjustment, I encountered a problem when the buttons have a predefined height, therefore, setting this cover screen to a high height, it will cover any buttons (of course, you do not need this if you do not close the buttons).
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/customSplash" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/backgroundColor" android:elevation="5dp" > <ImageView android:id="@+id/imageView" android:layout_width="202dp" android:layout_height="157dp" android:layout_centerInParent="true" app:srcCompat="@drawable/yourImage" /> </RelativeLayout>
In this example, my screen that I am trying to cover is a relative layout, so I can simply add and remove my splash screen to it using the addView and removeView view methods that I am trying to cover.
override fun onPause() { var parentView = findViewById<RelativeLayout>(R.id.parentView) var splashScreen = layoutInflater.inflate(R.layout.splash_screen_custom, null) parentView.addView(splashScreen, parentView.width, parentView.height) super.onPause() } override fun onResume() { var parentView = findViewById<RelativeLayout>(R.id.parentView) parentView.removeView(findViewById<RelativeLayout>(R.id.customSplash)) super.onResume() }
source share