Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: The splash screen does not get shown, why?

I am working on learning Android. From the documents I have read so far I can't figure out how to get the splash View to show (during the sleep the screen stays blank). It appears I need to start a new activity for the main layout, but this seems wasteful (the splash should be forever gone, I'd like to reuse its thread).

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Ext3 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Log.v("Ext3", "starting to sleep");

        try {
            Thread.sleep (5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.v("Ext3", "done sleeping");

        setContentView (R.layout.main);
    }

}
like image 295
kasterma Avatar asked Jun 10 '11 16:06

kasterma


People also ask

How do I set up splash?

First, run npx expo install expo-splash-screen . Next, add the following code to delay hiding the splash screen for five seconds. import * as SplashScreen from 'expo-splash-screen'; SplashScreen. preventAutoHideAsync(); setTimeout(SplashScreen.

Does Android have a splash screen?

Starting in Android 12, the launch animation for all apps when running on a device with Android 12 or higher. This includes an into-app motion at launch, a splash screen showing your app icon, and a transition to your app itself.

What is a splash screen setting?

A splash screen is a graphical control element consisting of a window containing an image, a logo, and the current version of the software. A splash screen can appear while a game or program is launching. A splash page is an introduction page on a website.


2 Answers

I believe your splash screen never gets shown because you never give the UI thread (that you are in) a chance to draw it since you just sleep there doing nothing.

Instead of the Thread.sleep, I would suggest you look into a Timer or something like that to schedule the refresh and changing the content of your view; an alternative would be to start an AsyncTask where you could sleep before changing the view as you are doing now.

Do not sleep or in any other way block the UI thread, that's bad... (causes ANR)

like image 84
Matthieu Avatar answered Nov 14 '22 22:11

Matthieu


When you call sleep like that, you are blocking the UI thread. Instead, put the second call to setContentView in a Runnable, create a Handler, and use the Handler's postDelayed method to run the Runnable. Something like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Runnable endSplash = new Runnable() {
        @Override
        public void run() {
            setContentView (R.layout.main);
        }
    }
    new Handler().postDelayed(endSplash, 5000L);
}
like image 30
Ted Hopp Avatar answered Nov 14 '22 22:11

Ted Hopp