In my android app, I need to show a common page whenever my app goes into the background so that when the user gets to see all the open apps on his Android device, the current page which will be in onPause state will not be shown, instead, my desired page is shown. I have observed this behavior in iOS. Need to implement in android.
2 Answers
There's an easy way to disable your app's preview when your app is shown in task switcher; FLAG_SECURE.
When this is enabled, your app won't display any previews, and will instead show a blank screen for most devices.
This can be done by creating a base Activity class that all activities extend, containing this in the onCreate:
protected void onCreate(@Nullable Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
super.onCreate(savedInstanceState);
}
Alternatively, you can selectively enable it on pause / resume (although the first approach is better):
override fun onPause() {
super.onPause()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
}
override fun onResume() {
super.onResume()
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
-
Thanks, JakeSteam for the answer. I have been already following this and I am getting the black screen. But my requirement is to show a custom page, in my case the splash screen, instead of the black screen. May 9, 2019 at 6:08
-
@Nishith Ah I see. I don't have direct experience with it, but you could just show some popover / fragment inside the onPause and hide it on the onResume?– Jake LeeMay 9, 2019 at 7:58
set a global boolean variable ;-
private boolean isPaused = false;
now set you onResume methods:-
@Override
public void onResume() {
if (isPaused) {
setContentView(R.layout.activity_splash);
isPaused = false;
}
super.onResume();
}
and now set this method:-
@Override
protected void onUserLeaveHint() {
isPaused = true;
super.onUserLeaveHint();
}
-
-
-
-
Actually, I tried this. I am using this method in my BaseActivity. I am setting my splash screen layout inside this method - setContentView(R.layout.activity_splash); When my app goes into the background it shows the splash screen - as expected. But when my app resumes, it gets stuck to this screen. It needs to resume with the page where I left. May 9, 2019 at 6:18