Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will thread be killed before the activity finishes in Android?

In Android, I have a thread that initializes a global variable. The thread starts when the activity starts. If the activity finishes before the thread initialized the global variable will the thread still run in the background to complete its job or it will be killed as the activity finishes?

like image 472
Samrakchan Avatar asked Jan 01 '12 09:01

Samrakchan


1 Answers

The Activity finishing is part of the main execution/UI thread in android. When you spawn a new thread, and perform operations on that thread, it works as a separate entity from the main UI thread.

Hence, to answer your question - The thread will still run in the background to complete its job.

However, a word of caution. If within the run() method, you are using some objects that are part of the activity class that just got terminated, you can run into null pointer exceptions.

like image 193
Gopal Nair Avatar answered Oct 04 '22 16:10

Gopal Nair