When I run this, It is supposed to draw the ui, (two simple images and a text static text field) wait five seconds, then advance to the next activity (another page with a few buttons)
I can get it to load the images and not advance, OR to show a blank page, wait five, then go forward.
The main java doc:
package com.example.ccbc_maps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//When I add a call to the main function here it just displays the white space
//Then advances after five seconds same thing if I paste the code within
//The main method.
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void main()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
}
Intent goForward = new Intent(this, CampusSelect.class);
startActivity(goForward);
}
}
And this is the class it advances to:
package com.example.ccbc_maps;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class CampusSelect extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campus_select);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_campus_select, menu);
return true;
}
}
Thanks for the help!
Things are not working as you expect because you’re forcing the main thread (UI thread) to sleep.
When you modify the UI, like call
setContentView()or modify the properties of your Views, they do not happen immediately. Instead, they get put in a message queue to be executed by the main thread later.You need to actually do the waiting in a separate thread. Probably the simplest way to accomplish what you’re trying to do is to use Handler.postDelayed() to schedule some work to be done on the main thread at a later time.