So, I just read this question: How do I make a splash screen? But instead of adding a fixed delay (like in the top answer), I wanted to keep the splash screen on while the MainActivity (with a MapFragment) loads.
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
synchronized (this) {
try {
wait(3000);
System.out.println("Thread waited for 3 seconds");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
try {
t.start();
t.join();
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I added the wait(3000) line, because I noticed before that the thread didn't live for long. However, if I make it wait for longer, there's just a black screen that lasts longer. For some reason, the SplashScreen activity won't show the ImageView. What should I do? Thanks.
Easy way to do it..
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.example.tabs.R;
public class Splash extends Activity implements Runnable
{
Thread mThread;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mThread = new Thread(this);
mThread.start();
}
@Override
public void run()
{
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}
}
splash.xml if you want to show a image
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/splash" >
</LinearLayout>
Note if you want to do some UI operation in splash .Then you have to create a handler and update UI in it.