23

I am developing an app that has private information and should not display a real screenshot in Android's recent app switcher. I've tried a variation of this solution, by setting the content view to an ImageView inside the onPause function, but it seems that the operating system takes a screenshot before the content view is changed to the custom image.

I am also aware of setting the window's layout parameter flags to secure, making the screenshot completely white, but I'd hope that there would be a way to customize the screenshot.

So, I'm wondering at what point that Android takes a screenshot of the app for the app switcher (specifically in KitKat and Lollipop).

Community
  • 1
  • 1
Louie Bertoncin
  • 3,744
  • 2
  • 25
  • 28

1 Answers1

13

EDIT

It is no longer possible to customize the screenshot which system uses to present the thumbnail in the recent apps.

Old answer

Take a look at method Activity.onCreateThumbnail - this is exactly what you're looking for as it let you draw your own thumbnail for Recent screen.

You get Canvas as one of the parameters in which you can draw (or not draw at all) directly. The main point is that you have to return true from this method, which indicates that system won't draw thumbnail itself.

The simpliest solution would be:

@Override
public boolean onCreateThumbnail (Bitmap outBitmap, Canvas canvas) {
    // Do nothing or draw on Canvas
    return true;
}

or if you want to draw your own Bitmap

@Override
public boolean onCreateThumbnail (Bitmap outBitmap, Canvas canvas) {
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myBitmap);
    canvas.drawBitmap(myBitmap, 0, 0, null);

    return true;
}
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • Could I please get a code example on how to use both the bitmap and the canvas? I also tried that earlier and did not know how to manipulate the two. – Louie Bertoncin Jun 23 '15 at 19:42
  • @LouieBertoncin added example – Dmitry Zaytsev Jun 23 '15 at 19:43
  • Ok, here's what I did to use the two parameters: `outBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myBitmap);` `canvas.drawBitmap(outBitmap, 0, 0, new Paint());` – Louie Bertoncin Jun 23 '15 at 19:52
  • @LouieBertoncin you must not modify incoming `outBitmap` - it is already created by the system. Java is still pass-by-value, so by replacing `outBitmap` reference you're not changing anything. Instead, you must draw your `Bitmap` directly on `Canvas`. http://stackoverflow.com/questions/13361231/android-draw-bitmap-within-canvas – Dmitry Zaytsev Jun 23 '15 at 19:54
  • The method never seems to be called for me. I have a log statement inside the body that never prints. Is there any sort of setup for this method? – Louie Bertoncin Jun 23 '15 at 20:28
  • @LouieBertoncin are you still using `FLAG_SECURE`? As far as I know, it will prevent system from calling `onCreateThumbnail`. – Dmitry Zaytsev Jun 23 '15 at 21:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81414/discussion-between-louie-bertoncin-and-dmitry-zaitsev). – Louie Bertoncin Jun 24 '15 at 13:01
  • For some reason, the onCreateThumbnail() method never got called, but this should be the correct answer and it is thoroughly explained. Thank you @DmitryZaitsev – Louie Bertoncin Jun 26 '15 at 13:58
  • 9
    This approach is not working also because onCreateThumbnail method is not called. – Gunhan Nov 19 '15 at 18:56
  • so how can we achieve custom image for recent history list ? Any workaround ? – james Jun 16 '16 at 13:05
  • 1
    I'm pretty sure it is possible, because I could see it working on [CommerzBank application](https://play.google.com/store/apps/details?id=de.commerzbanking.mobil). – azizbekian Apr 19 '18 at 15:21
  • From Activity documentation for onCreateThumbnail(): "This method was deprecated in API level 28. Method doesn't do anything and will be removed in the future." – happydude Oct 11 '18 at 10:29
  • @azizbekian I checked now - CommerzBank app just set FLAG SECURE. App disable screenshots for the whole application – Alesh17 Oct 04 '22 at 11:56