Daniel Bystrican
Daniel Bystrican

Reputation: 131

Activity doesn't load layout

I setted up simple application with 2 activities. I had problem in first activity. It is used just for displaing logo for 3 seconds and then launching second activity. Problem is that it doesn't load the layout, it waits 3 seconds and then load second activity. The code is here:

public class StartActivity extends Activity {

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.start_screen);
        final Intent myAct = new Intent(this, MyActivity.class);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
           }

        startActivity(myAct);
        finish();

    }
}

I was able to fix this problem by creating another thread and executing waiting there. Fixed code is here:

new Thread(){
            public void run(){
                try {
                    Thread.sleep(3000);


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(myAct);
                finish();
            }

           }.start();

So i actually fixed the problem. I just want to know why it works the second way but it doesnt work by first way becouse i don't understand it.

Upvotes: 2

Views: 1218

Answers (3)

Vitaliy A
Vitaliy A

Reputation: 3838

Actually your approach is also wrong / bad practice. Starting new activity and finishing current should be done on main thread. Also try to avoid final objects initialization, they may stack in memory. Here is one of correct approaches (postDelayed() will be perform on MainThread):

public class StartActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_screen);

View view = findViewById(R.id.any_view_from_start_screen_layout);
view.postDelayed(new Runnable(){
    public void run(){
    startNewActivityAndCloseCurrent();
},3000);


}
private void startNewActivityAndCloseCurrent(){
Intent myAct = new Intent(this, MyActivity.class);
    startActivity(myAct);
    finish();
 }
}

Upvotes: 1

meShakti
meShakti

Reputation: 273

First method makes the UI thread sleep .This result in stopping of all the functioning and UI interaction of the activity. The second method uses another thread .Since it's the other thread which sleeps all the UI parts of the main UI thread works fine and the code works as intended.

Upvotes: 1

joao2fast4u
joao2fast4u

Reputation: 6892

In the first case, you are telling the UI Thread to sleep, preventing it to draw the layout. When it finishes the sleep process, it immediately starts the next Activity. That is why you do not see the first Activity layout being shown. This approach is not recommended at all as during the sleep time, your application is not responsive to any user actions. You are recommended to use an AsyncTask or a new Thread or a Handler, using postDelayed() method but never cause the UIThread to stop doing its job (drawing and handling UI events).

In the second case, you are making the sleep() in a new Thread, not the UIThread. So, what happens is that the UI Thread is never interrupted and is allowed to draw the entire layout. At the same time, the sleep time is respected as well. Once the sleep() ends, your new Activity starts.

Upvotes: 4

Related Questions