Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to an Android thread after the Activity that created it is destroyed?

In my Android app, the main Activity will sometimes start a Thread to load data from a server. This thread modifies the app's database and edits some important files. AFAIK, it would appear that this thread continues to execute. What will happen to this thread if the Android gets into a low memory situation and decides to kill the entire Application? Will there ever be a situation were this thread might die prematurely? If so, is there any way I can see that the thread is being killed, and do something about it?

I'm asking because this thread modifies important data in the database, and if it is suddenly killed, the application could stop functioning properly.

like image 364
Zarjio Avatar asked Jun 01 '11 22:06

Zarjio


People also ask

Which method is called when the activity of a container is destroyed in Android?

onDestroy() : This gets called before the Activity is destroyed. The system calls this method when a user terminates the Activity, or because the system is temporarily destroying the process that contains the Activity to save space.

When can Android activity be destroyed?

The Activity lifecycle is especially important because whenever an activity leaves the screen, the activity can be destroyed. When an activity is destroyed, when the user returns to the activity, the activity will be re-created and the lifecycle methods will be called again.

How does the threading work in Android?

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).

What happens when a thread finishes execution?

So when does a thread finish? It happens in one of two cases: all instructions in the Runnable are executed. an uncaught exception is thrown from the run method.


2 Answers

AFAIK, it would appear that this thread continues to execute.

This is true but you have no guarantee of how long the thread will stay alive.

What will happen to this thread if the Android gets into a low memory situation and decides to kill the entire Application?

This is actually a fairly rare case in my experience but it will depend on the device's available memory and the user's behaviour, for example they use the device heavily and start multiple apps.

Will there ever be a situation were this thread might die prematurely?

Yes

If so, is there any way I can see that the thread is being killed, and do something about it?

No

I'm asking because this thread modifies important data in the database, and if it is suddenly killed, the application could stop functioning properly.

What you describe could be classed as something which is 'mission critical'. As the other two answers have pointed out, a Service would be a more robust way of doing things as a Service is one of the last things to be 'killed' in a low memory situation. Using START_REDELIVER_INTENT may help in resuming what it was doing.

In any case, if you have a 'mission critical' operation, you need to design your code for full recovery such as the use of transactions and the possibility of rollbacks in case of errors.

like image 123
Squonk Avatar answered Oct 18 '22 18:10

Squonk


It sounds like you should move the db updating to a Service. Once an Activity goes to the background, Android assumes its process can be killed off if necessary and restarted later without ill effect. See Application Fundamentals for more info.

like image 35
Ted Hopp Avatar answered Oct 18 '22 18:10

Ted Hopp