Thursday, June 4, 2015

Foredroid - Detecting when your Android app goes background / becomes foreground - Android

Detecting when your Android app goes background / becomes foreground,
is not so simple task as you might think...

I found this utility git for detecting and notifying this:
https://github.com/steveliles/Foredroid

Please note, that it won't work correctly for all case, but it's still very useful.
For example a share intent, if the share dialog is open and your app is still visible in the background, it won't work.
But after selecting an app to share with, it will.

Anyway, It was perfect for what I needed.

Wednesday, June 3, 2015

Control Android's recent apps screenshot or disable screenshots in Android

Sometimes you have a situation when you need to control the screenshot that is shown in Android's task switcher / recents apps menu,



For example: you have an application with a lock screen, and when the user lock his session and the application moves to background, you don't want to accidentally show the last application activity in the recents apps menu.

Sooo, You have 2 options:
  1. Exclude your app from the recents apps menu
    You can do it by adding next code in the manifest.xml file under the <activity> tags:
     android:excludeFromRecents="true"

  2. Disable screenshots of your app
    You can disable screenshots of your app, and as a side effect, 
    The android system itself, won't be able to take a screenshot of your app last session.
    You can do it by:

    public class MyActivity extends Activity 
    {
      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(LayoutParams.FLAG_SECURE,
                            LayoutParams.FLAG_SECURE);
        setContentView(R.layout.main);
      }
    }
    

    Please note that in this option, your app will still be in the recent apps menu, 
    but with a blank (white or black) screenshot thumbnail.

    In Addition, when you will try to get screenshot you will get this message: