Android: ¿Qué sucede si el hilo principal finaliza antes que el hilo de trabajo?

To implement my splash screen for my app, I'm starting a thread from main thread that sleeps for a while and calls the home intent. And is working fine. But my concern is Do we need to wait on main thread? (Currently I'm not doing this) If main thread finishes first, what will happen and how to handle that scenario?

Gracias, Venkat

preguntado el 09 de marzo de 12 a las 17:03

Main thread can't finish before "child" thread is running, This is a question of variable scope lifetime. So "child" thread is created and initialized in scope of the main thread so whilst it is alive - main thread will be alive as well -

2 Respuestas

Use this code for splash screen:

Handler handler=new Handler();
Runnable r=new Runnable()
    public void run() 
        // start another activity                       
handler.postDelayed(r, 5000); // wait 5 second

respondido 09 mar '12, 17:03

This is the correct answer. No need to start a thread when a runnable can do the same thing for you in a handler! - Josh

The main thread itself cannot be "waited on". The application cannot exit while there are still non-daemon child threads still running.

If you set the child threads to be daemons prior to starting them, they will be immediately terminated when the application exits.

respondido 09 mar '12, 17:03

Thx Tudor, so I need not to wait on main method, and my existing code is safe enough, right? - Papana Venkat

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.