Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setContentView execute too late

Tags:

android

I have a problem, I make a simple application to show you my problem. I want that setContentView executes and displays the .xml BEFORE the Sleep is executed. I thought everything will be execute in order? Is there anyone how can say me why it doesn't do that?

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // do something
    }

Thanks a lot!

EDIT:

Here is the real OnCreate, seems to be a bigger problem.

Everything with the sleep worked fine, but with the Connect method there are problems.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

ConnectBluetooth();

}   

In the ConnectBluetooth() method, I just create a new Socket and try a connection.

With a ned thread or a handler it doesn't seems to work, what should I do then? Use something like an asynctask?

Thanks a lot in common!

like image 577
user1927295 Avatar asked Aug 01 '26 19:08

user1927295


1 Answers

The layout isn't displayed until after the creation process has finished, after onResume() is called. However there is no callback for when the layout is displayed, but you can use a Handler and Runnable to do this.


Create a couple field variables:

Handler handler = new Handler();
Runnable delay = new Runnable() {
    @Override
    public void run() {
        // Do something 
    }
};

And onCreate() call:

handler.postDelayed(delay, 10000);
like image 98
Sam Avatar answered Aug 03 '26 10:08

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!