Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show splash screen when app is in background in Android?

I have a Splash screen(Couldn't avoid it, as it is for branding reasons) for my application.

I want to mask the user interface and show the splash screen when it is in background (like banking applications do).

Should I overide onPause() and onResume() for the view in MainActivity?

Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

Splash Activity : onCreate()

setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            /* Create an Intent that will start the Main-Activity. */

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}

MainActivity(): Just show a layout with text and buttons

onCreate():

setContentView(R.layout.example_view);

Now when my application is in background and When I press Menu button to see list of applications in my stack, I should see the splashview(the xml file) not the deafult behaviour i.e MainActivity layout.

One of the things I tried out is adding

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE); 

which does mask the application when it is in background with White Screen but I'm trying to show the splash view when the app is in background.

I know all the iOS Banking application has this feature.

Edited : enter image description here

If you see the twitter app(Example) in the background, I want to mask the application view with splash screen. Right now using FLAG_SECURE helps me to mask the screen with white screen but I want to show Splash layout rather than white screen.

  • If you see the below picture from an iOS device - DISCOVER application (example) masks the app with splash screen, Can this be done on Android?

enter image description here

like image 782
DroidDev Avatar asked Oct 30 '22 09:10

DroidDev


1 Answers

Can this be done on Android?

Not directly, AFAIK.

Should I overide onPause() and onResume() for the view in MainActivity?

Probably not. While you are welcome to change your UI there:

  • Those methods get called for other reasons (e.g., when the user taps on another window in multi-window mode, when you launch another of your activities)

  • That may be too late with respect to when the screenshot is taken

like image 61
CommonsWare Avatar answered Nov 15 '22 07:11

CommonsWare