Henrik
Henrik

Reputation: 1157

What does "setContentView" really do?

In my main activity, I would like to have it set up, so that I first get met by a contentView just showing a background and some text. After X seconds, I want to change to my other view (GLSurfaceView).

This is obviously something I am doing completely wrong.

This is how I've imagined it could've been done (it's all in the onCreate method):

setContentView(R.layout.main);

try {
    Thread.sleep(10000);
} catch (InterruptedException e) {
}

viewer = new Viewer(this);

setContentView(viewer);

Where layout Main is what I want to show at the beginning and Viewer is my GLSurfaceView class.

What happens is that it just goes black for 10 seconds and then it starts loading the objects I've got that is shown through OpenGLES.

There's nothing wrong with the layout Main, since it works if I just erase the lines under where the Thread.sleep takes action. Though, nothing happens before the Thread.sleep is over...

With that said, my questions are following:

Upvotes: 5

Views: 12709

Answers (5)

Bry M.
Bry M.

Reputation: 135

You're not sleeping the UI thread in the way you think you are.

The simplest thing for what you're looking to achieve is to separate the views into separate activities and let Android handle the transition between the views. It adds another file to your codebase, but it's fairly straightforward. Let's say your initial, plain view (R.layout.main) is for a SplashActivity activity, and your post-splash view goes into PostSplashActivity. Then you could do something like this:

public class SplashActivity extends Activity {
    private static long DELAY = 10000; //milliseconds;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new Handler().postDelayed(
            new Runnable() {
                @Override
                public void run() {
                    Intent postSplash = new Intent(SplashActivity.this, PostSplashActivity.class);
                    SplashActivity.this.startActivity(postSplash);
                    SplashActivity.this.finish();
                }
            }, DELAY);
    }
}

This will draw your R.layout.main layout, and then puts a startActivity call for your PostSplashActivity on the message queue and tells the queue to wait DELAY milliseconds to execute it.

Upvotes: 2

barry
barry

Reputation: 4147

It sounds like you want something like a splash screen. I like to think of these as separate to the following screen, so always use a separate activity rather than calling setContentView twice. You'd still need to sleep in a thread.

Just personal preference though...

Upvotes: 0

sampathpremarathna
sampathpremarathna

Reputation: 4064

Don't make sleep the main thread(UI thread).Use a threads,AsynkTask or TimerTask for that type of works instead.

Upvotes: 2

Emil Davtyan
Emil Davtyan

Reputation: 14089

I'm assuming this in your onCreate() and thats why you are seeing nothing.

The way I would implement this is to start a thread using AsyncTask sleep in the doInBackground and in the onPostExecute set up the new view.

Upvotes: 10

coder_For_Life22
coder_For_Life22

Reputation: 26981

It seems like you are making the main thread sleep. This may be why the code is running tell after.

Upvotes: 0

Related Questions