What does “setContentView” really do?

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

This is obviously something i am doing completly wrong.

This is how i've imagined it could've been done (It's all in the onCreate)

    setContentView(R.layout.main);

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

    viewer = new Viewer(this);

    setContentView(viewer);

Where layout Main is want i want to show at beginning and Viewer is my GLSurfaceView class.

What happens is that it is just beeing black for 10 seconds and then it starts loading my objects i've got that is shown through OpenGL(ES).

There's nothing wrong with the layout "main" since it works if i just erase the lines under where the thread sleeps take action. Tho nothing happens before the Thread.sleep is over...

With that said, My questions are following

  • Why is the contentView not changing until after Thread.sleep is done?
  • What would be an appropiate solution to what i want to achiev?

Answers


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.


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


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.


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


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...


Need Your Help

Looking for guidance rewriting specific URL's

php .htaccess mod-rewrite

I have changed the CMS to my site and it has rewritten the URLs to be more friendly but I have 20 pages which are linked on different sites across the web. I want when people click those old links ...

android add a new place to Facebook

android facebook checkin

i've build an android application which inside you can check in to a Facebook place.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.