Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Application Logo Activity

I have a what seems to be a simple question but for the life of me can't figure this out...

I have a main Activity that displays the company logo, essentially a splash screen. I want this to display for 2 seconds or so and fade out to the actual application main activity. I've tried to implement using sleep however doing this gives me a blank screen for the logo activity. It seems the image isn't loaded until AFTER the sleep is done. So essentially the app starts, displays a black screen for 2 seconds, then transitions into my application. If I click back then I see the logo. What am I doing wrong here? This is my logo code. The logo.xml has a single ImageView with the drawable resource:

public class Logo extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logo);

        // Intent to jump to the next activity
        Intent intent= new Intent(this, NextActivity.class);
        this.startActivity(intent);

        SystemClock.sleep(2000);
    }
}
like image 467
goodnoodle Avatar asked Feb 20 '23 02:02

goodnoodle


1 Answers

You are blocking the UI thread, which is a big no-no. The system cannot paint the screen until your onCreate method returns. A common way to do what you want is to start a separate thread that waits and then posts a Runnable to the UI thread:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logo);
    final Handler handler = new Handler();
    final Runnable doNextActivity = new Runnable() {
        @Override
        public void run() {
            // Intent to jump to the next activity
            Intent intent= new Intent(this, NextActivity.class);
            startActivity(intent);
            finish(); // so the splash activity goes away
        }
    };

    new Thread() {
        @Override
        public void run() {
            SystemClock.sleep(2000);
            handler.post(doNextActivity);
        }
    }.start();
}

A somewhat simpler way (as Athmos suggested in his answer) is to let the handler do the countdown for you:

Handler mHandler;
Runnable mNextActivityCallback;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logo);
    mHandler = new Handler();
    mNextActivityCallback = new Runnable() {
        @Override
        public void run() {
            // Intent to jump to the next activity
            Intent intent= new Intent(this, NextActivity.class);
            startActivity(intent);
            finish(); // so the splash activity goes away
        }
    };
    mHandler.postDelayed(mNextActivityCallback, 2000L);
}

This has the advantage that you can cancel going on to the next activity (if, say, the use presses the Back button or if you detect an error condition or something during those 2 seconds):

@Override
protected void onPause() {
    if (isFinishing()) {
        mHandler.removeCallbacks(mNextActivityCallback);
    }
}
like image 182
Ted Hopp Avatar answered Mar 05 '23 15:03

Ted Hopp