1

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 2

2

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)
}
2
  • 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 Lee
    May 9, 2019 at 7:58
0

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();
    }
4
  • Where it needs to be called ? May 7, 2019 at 10:34
  • in your all activity May 7, 2019 at 10:34
  • have you set your common view in this method May 8, 2019 at 9:14
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.