2

On executing the following code, i found that the entire app freezes for 10000ms before showing anything on the emulator's screen. I would have expected the first Toast message to appear , followed by the app to freeze for 10000ms and the second toast message to appear. makes me wonder if android piles up all the code in the 'oncreate' method before executing it. is it supposed to be that way?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, new ServiceCode("Hi").s, Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Toast.makeText(this, new ServiceCode("Hello").s, Toast.LENGTH_SHORT).show();
        }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • 1
    what's the overall performance of your emulator? – nikis Feb 21 '14 at 06:21
  • Check with System.out.println("Your Message"); , ideally it should execute as a normal function. Executing one LOC after the other. – Skynet Feb 21 '14 at 06:27
  • what is there in ServiceCode class? – Aparupa Ghoshal Feb 21 '14 at 06:28
  • dont know how to measure the performance of emulator. If I change the sleep time to 10ms - the toast message shows up almost instantaneously, which makes me think that the performance of emulator is probably not determining the manner in which the code inside oncreate is being executed – user3335883 Feb 21 '14 at 06:28
  • Beworkers answer is the correct one - but also, you should only use `thread.sleep()` if you **really** understand threading and never on the main thread. – Simon Feb 21 '14 at 06:34

6 Answers6

2

It behaves as expected. There is a single thread responsible for UI updates. It's called main thread. This thread shows toast messages too. When you call Toast.show(), Android schedules a new task for the main thread. When main thread is done with onCreate(), it will execute this task and show the toast. But because you blocked main thread for 10 seconds, no toasts are show. There is no one free, who can show this message. But then, 10 seconds later, both toasts will appear one after another, because main thread is free to show them.

Best practice is to never block the main thread. Otherwise your application will freeze and users will see ANR (application nor responding) message. If you need to execute something later in time, you need to post this task to the main thread's task queue for been executed later.

The code below will behave as you expect.

public class MainActivity extends Activity {

    private Handler handler = new Handler();

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

        // show first toast immediately
        Toast.makeText(this, new ServiceCode("Hi").s, Toast.LENGTH_SHORT).show();

        // schedule second toast to appear 10 sec later
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, 
                    new ServiceCode("Hello").s, Toast.LENGTH_SHORT).show();
            }
        }, 10000);

    }
}
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
1

When an Activity is created, the system-process would send a message to the UI thread of the Activity. The UI thread of the the Activity received the message and then executes "onCreate" method.

Here you make a toast in the "onCreate" method. That will not show the toast immediately. It only sends a message to the message queue of the UI thread. After you UI thread have fininshed the "onCreate" "onStart" "onResume" method, it receives the message of "Toast". At that moment, the Toast is actually showed on the screen.

Ethan J
  • 281
  • 1
  • 9
0

The reason is simple, the show method of the Toast class might not be a synchronous call "internally", what I mean is, the main-thread is very unlikely to wait until the Toast is actually shown and rendered to continue, hence, it might start a functionality to start rendering the Toast BUT since you immediately after that force the main-thread to stop, it doesn't handle that request since main thread have the highest priority.

Hope it helps!

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
0

check these links to know about the Android Life-Cycle

http://developer.android.com/training/basics/activity-lifecycle/index.html

Android activity life cycle - what are all these methods for?

your creating a Splash Screen,

if not remove

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

you App won't freeze.

Community
  • 1
  • 1
Rajesh Mikkilineni
  • 854
  • 10
  • 22
0

Try this :

Toast.makeText(this, new ServiceCode("Hi").s, Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
        Toast.makeText(this, new ServiceCode("Hello").s, Toast.LENGTH_SHORT).show();
        }
      }
Aparupa Ghoshal
  • 309
  • 2
  • 10
0

The display of Toast is an asynchronous call.Thus, once the toast request is executed, the operating system jumps to the next operation and meanwhile the toast is prepared and displayed. In your case since the next operation blocks the UI Thread for 10 sec the toast is not displayed until the UI Thread is released.