Android splash screen while loading MainActivity [duplicate]
Asked Answered
S

3

3

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.

Sunset answered 25/10, 2013 at 2:48 Comment(2)
Splash screens are evil, don't use them!Thibaud
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Syblesybley
K
-3

make a splash screen like this:

while(counter < 1300) {
    try {
            Thread.sleep(1000);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        counter+=100;
    }

    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
    startActivity(intent);

Hope it helps!

EDIT: Anotehr way to make a splash would be like this:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
         Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
         startActivity(intent);
    }
},3000); -> the splash will show for 3000 ms, you can reduce this.
Kerakerala answered 25/10, 2013 at 6:50 Comment(7)
Will try it! I'll make sure to tell you if it does. Thanks.Sunset
I didn't give you the -1, I accepted the answer. (:Sunset
@HugoM.Zuleta Lol it wasnt you :).. someone else did. :).. i just wanted to know the reason so that i could improve if i was wrong..Kerakerala
reason ? cos it's not correct splashLawanda
Purpose of splash screen is to show the logo till the activity is loaded. Its not meant for a fixed time showing. On slower phones, the splash screen will appear longer and in faster phones it will just flash.Rillet
@nizam.sp: well if u have a better way why not post your answer? Instead of just negating this solution, or whoever it did.Kerakerala
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Syblesybley
B
4

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.

Banlieue answered 22/4, 2014 at 5:39 Comment(9)
this is not the best way.Suprarenal
Yes not the best way. there are other ways like TimerTask,Handler,CountDownTimer. Handler is preferable.Banlieue
Those aren't the best ways either.Suprarenal
bignerdranch.com/blog/splash-screens-the-right-way this guy has shown the best way.Suprarenal
I have read that article before but it didn't provide any delay and splash doesn't work as it should be.Banlieue
that's the actual technique. You should check the splash of Maps or Youtube app. They are using this kind of technique.Suprarenal
oh i see you work at BoardPeak :D . how can i approach you? email if possible pleaseSuprarenal
Yes sure, here you go [email protected] .How may I help you?Banlieue
I just e-mailed you.Suprarenal
T
1

The main thread cannot be blocked for a long time. You should use Handler to trigger another event if you want to start another event in 3 seconds. You can use sendMessageDelayed. In addition, startActivity should be called in main thread.

Towline answered 25/10, 2013 at 6:41 Comment(0)
K
-3

make a splash screen like this:

while(counter < 1300) {
    try {
            Thread.sleep(1000);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        counter+=100;
    }

    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
    startActivity(intent);

Hope it helps!

EDIT: Anotehr way to make a splash would be like this:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
         Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
         startActivity(intent);
    }
},3000); -> the splash will show for 3000 ms, you can reduce this.
Kerakerala answered 25/10, 2013 at 6:50 Comment(7)
Will try it! I'll make sure to tell you if it does. Thanks.Sunset
I didn't give you the -1, I accepted the answer. (:Sunset
@HugoM.Zuleta Lol it wasnt you :).. someone else did. :).. i just wanted to know the reason so that i could improve if i was wrong..Kerakerala
reason ? cos it's not correct splashLawanda
Purpose of splash screen is to show the logo till the activity is loaded. Its not meant for a fixed time showing. On slower phones, the splash screen will appear longer and in faster phones it will just flash.Rillet
@nizam.sp: well if u have a better way why not post your answer? Instead of just negating this solution, or whoever it did.Kerakerala
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Syblesybley

© 2022 - 2024 — McMap. All rights reserved.