Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ResultReceiver what happen if activity is destroyed and intent service is still running

This question has already been asked here but yet no good answer.

So basically I have an intent service running in the background to do some stuff and once finished I send the result back to activity using resultreceiver so what I need to know is the following:

  1. How can I handle a situation when activity is destroyed while intent service is still running?
  2. How to know if activity is destroyed from intent service?
  3. What happens to resultreciever when sending the result back to activity when the activity is already destroyed? Does that produce an error?
like image 507
has19 Avatar asked Jul 30 '16 12:07

has19


People also ask

What is the difference between intentservice and resultreceiver?

The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf (). ResultReceiver is a generic interface for receiving a callback result from someone. The ResultReceiver class is just a simple wrapper around a Binder that is used to perform the communication.

How to refresh activity when intentservice is finished in Android?

This example demonstrates How to refresh Activity when IntentService is finished. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view, ...

What is a resultreceiver in Android?

A ResultReceiver is making it easier to get the result from a (Intent)Service. It is an interface we implement and pass it to the IntentService through an Intent. IntentService will then fetch this object and call its receiver.send function to send anything (in Bundle) to calling Activity.

What thread does the intentservice run on?

The IntentService runs on a separate worker thread. The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent () is called on this thread. The IntentService cannot run tasks in parallel.


1 Answers

This is a late answer, but I was researching the functioning of IntentService and I came across your question.

how can handle a situation when activity is destroyed while intent service is still running ?

Since the IntentService is a separate component it will continue to run until the task designated to it is complete or the process on which the application is running is destroyed. The initial thought might be to stop the IntentService when the activity is destroyed. Simple, isn't it? Well, not quite. As it turns out when you call stopService(Intent), though the onDestroy() method of the IntentService is called, the background thread will continue to run until it's complete and deliver a result to the receiver.

how to know if activity is destroyed from intent service?

This is a good question and something that I myself wondered. One neat solution is described here - IntentService responding to dead ResultReceiver

what happen to resultreciever when sending the result back to activity when the activity is already destroyed ?does that produced error?

This will most likely not result in any visible exception as the activity is not visible. But it could result in a memory leak on a configuration change as you have a reference to an object defined in the activity (which will prevent the activity from being garbage collected as long the thread continues to run - see IntentService prevents activity from destroying). But the answer linked above should alleviate this problem as it nulls out the reference of the ResultReceiver in onDestroy, avoiding any potential memory issues

Also, it would be worthwhile to mention that you can consider the approach of a LocalBroadcastManager which makes it easier to work with the lifecycle of the Activity by registering/unregistering the BroadcastReceiver. Example available here from the google samples repo

like image 175
Abhijit Avatar answered Sep 30 '22 15:09

Abhijit