goodnoodle
goodnoodle

Reputation: 235

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);
    }
}

Upvotes: 4

Views: 3686

Answers (4)

Ribson
Ribson

Reputation: 15

You should use sleep in new thread because it stops to execute the oncreate method and you can use it after setting the setcontentview

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

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);
    }
}

Upvotes: 8

trichner
trichner

Reputation: 850

using onResume() and a Handler, the easy and clean way

Have you tried to put this...

// Intent to jump to the next activity
Intent intent= new Intent(this, NextActivity.class);
SystemClock.sleep(2000); //NOTE before the start of the next activity
this.startActivity(intent);

...in onResume();

As far as I know, it's not the idea that the Activity is displayed right when onCreate() is called. But onResume() gets always called and as far as i know everthing should be displayed by then. If you want to show it only once, make your flag for it. see: Activity lifecycle

But anyway, what you're doing it the wrong way ;) use a Handler, Thread or similar for this

Other solutions:

  • Why not display an ImageView infront of everything and just hide it after 2s? you could just use a Handler for your delay. Or even use transparency to make a fancy fadeout. Look at ImageView setAlpha(...).
  • Why not use a Handler instead of the sleep(...). IMHO sleep(...) could result in a 'Application Not Responding'

this code should to the job:

Handler mHandler = new Handler();

class MyTask implements Runnable{
    public void run(){
        //start next activity or fadeout ImageView or ...
    }
}

now call in onResume:

mHandler.postDelayed(new MyTask(), 100); //or any other Runnable

Upvotes: 0

KatGaea
KatGaea

Reputation: 1026

Instead of using sleep in the constructor, Which delays the construction of the view by that 2 seconds, which is why it only shows the picture AFTER 2 seconds, try following the advice from here

http://androidforums.com/40306-post4.html Also for the back button problem add

android:noHistory="true"

to the manifest for the Logo Activity

Upvotes: 0

Related Questions