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);
}
}
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.
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.
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.
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)
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With