Service onDestroy() called when activity started from another one - android

Firstly, sorry for my English if it's not enough good. I'm having some problems in my application.
Starting, my app has multiple activities and one service which works in background since the first activity execute it. If I press back button on my root activity, I exit from the app but the service continue working. Then, I go into the app back, and the service work perfectly. My problem comes when I press a button to exit the application (there, I stop service and finish the root activity mainly) and then exit without any problem, when I want to enter the app again, the service is started, but if I want to change to another activity (which doesn't have the serviceConnection) my service get called onDestroy() method without any reason for that. I don't have how to continue, because the usual way to execute in this case is the service go on working as the first case.
Thanks a lot.

There is for sure a reason why onDestroy gets called.
In the first section of 'Services' in the developer guide, you can read the following:
Multiple components can bind to the service at once, but when all of
them unbind, the service is destroyed. (link)
So, if all components unbind from the service, the service will get destroyed. When you enter the activity that is not bound to the service, the service will be destroyed.
I'm wondering why you don't want your service to be destroyed since you don't need it in your 'another' activity?

Related

How is a running IntentService managed after the app process is killed

Just trying to clarify my understanding of how an IntentService is managed by the OS once terminating states have been reached. By terminating, I mean when the current activity is destroyed or the app process is killed, as per the following documentation:
https://developer.android.com/guide/components/activities/activity-lifecycle
Given the comment
Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask
at https://developer.android.com/training/run-background-service/create-service;
I feel as if:
1) A started IntentService is unaffected by the activity lifecycle. Is this correct?
2) If (1) is true, will it continue to run indefinitely even after a terminating state is reached, up to some point that it either stops itself or the OS decides to stop it?
In my particular situation, I'm using an IntentService during app startup to query APIs, grab content, and then add a new (landing) Page to the Xamarin.Forms navigation stack (this would be equivalent to starting a new activity).
This leads me to my next question...
3) What happens if the app is already in a terminated state when it comes time to the IntentService creating a new Activity? Surely the Activity can't be added to the navigation stack as it no longer exists once the app is terminated?
Yes, a started IntentService is unaffected by the Activity Lifecycle. Actually, all Services outside of bound Services are unaffected by the Activity Lifecycle.
An IntentService will continue until it reaches completion of it's work, the application is destroyed, or if the System decides to kill the Service due to the changes in the Android 8.0 background Service rules.
Your use of terminated state is too broad... If the Application is already terminated, then nothing will happen because the IntentService would have been terminated already too. If it's the Activity that launched the IntentService that was terminated, then nothing happens, since by default, an IntentService has nothing to do with Activities, even if it's the one that started it.
For the last question, it really depends on how you choose to communicate the result of IntentService to an Activity.
If you're using a BroadcastReceiver, then nothing will happen because an IntentService will fire the broadcast without any problems, but the Activity won't be able to receive the results since it's terminated.
But if you're simply creating a new Activity, then you can simply use startActivity() with the result data added to the Intent. Though, I doubt the user will be happy to see an Activity suddenly open on the screen when they're no longer in your app. Starting a new Activity has nothing to do with a previous Activity, since any instance of a Context can start an Activity.
Honestly, based on your question, it sounds like you're very concerned with an IntentService and it's connection with the Activity that started it. If that's the case, you really shouldn't be using an IntentService, since that's not really it's purpose. It's not meant to have a connection with an Activity. It's simply meant to do work and finish.
Instead, a bound Service would be a better option since it has a direct connection with the Activity that started it.

How to kill app once focus is lost?

I'm trying to get the main activity for my app to finish (not remain an active application in background) while keeping the process alive for a service.
If the user presses the home button and leaves my app or another activity is launched from outside of my app, I want the activity to be closed and NOT listed as an active application.
It is important, however, that the process stays alive. I don't want my background service to die as well.
What's the best way to go about doing this?
You should not forcibly close the application as the system does well in handling this itself. Instead you should call finish() to signal that the app is done and can be disposed of(your service will continue running).
By default Services don't have a UI. Once started they run until they crash or are killed . The user can close your app and launch a new one and the Service will persist.
Activities on the other hand are only running when they are visible. When the user navigates to another activity the previous activity is paused, stopped, or killed.
A simple way to do what you've briefly described above would be to create an Activity that starts a Service. That way when use navigates away form your Activity the Service will keep running in the background.
If you want your activity to die completely whenever a new Activity comes into view simply put a call to finish() in the onPause() or onStop() methods for your activity (which ever is more appropriate for your app).

Get the status of one application from another application

I have two applications such as Activity and Service. I start Service from Activity. When I exit the activity it stops but it is not completely getting stopped, running in background(Could see in Settings->ManageApplications menu).
So I used android.os.Process.killProcess(android.os.Process.myPid()) in onDestroy() function which completely terminates application. But the issue is that it also stops my service since it was started from my activity.i also tried using the below lines,
ActivityManager activityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(this.getApplication().getPackageName());
But still it stops my service since it will stop the associate processes as said in document.My question is that how can i terminate my activity completely without killing my service. Thanx in advance.
Scenario
Assume there are two applications 'a' & 'b'. I stop Application 'a' using finish() call, 'a' closes successfully. Now I check the status of 'a' from 'b' using runningProcessInfo call and i still find that application 'a' is running in the background.
My question is that how can i terminate my activity completely without killing my service.
Call finish(), which happens automatically when the user presses BACK (by default).
When i exit the activity it stops but it is not completely getting stopped, running in background(Could see in Settings->ManageApplications menu)
An application is not an activity. An application is a collection of components. In your case, your application consists of at least one activity and at least one service, based upon the information in your question.
So i used "android.os.Process.killProcess(android.os.Process.myPid())" in onDestroy() function which completely terminates application.
Do not do this.

Bind to service from android activity

I have an Activity that starts a service. When the activity is closed, I want the service to continue running in the background. I have a couple of questions here.
Will closing the activity screen cause the activity to actually stop? Or do I need to forcibly stop it to cause it to stop?
If closing the screen does cause it to stop, then I assume I need to use startService to start it. Is that correct? If that is the case, is there a way to get a handle to the running service next time the activity starts? If it is not the case, then I can just bind to the service.
When your Activity is no longer visible on the screen, it is stopped. Stopping an Activity bound to a Service does not stop the Service. However, you'll want to make sure you unbind from the Service when your Activity calls onDestroy() to make sure you don't have any dangling handlers and suchlike.
You can rebind to a running Service the same way as you did the first time. Sending an Intent to start a Service that's already running doesn't create a second instance of it, so that's safe.
A good way to start a Service like you're describing is to start it using the Context.BIND_AUTO_CREATE argument to your call to bindService().
The details of all of this can be found at the Android docs about bound services. It can be a little confusing at first. Follow the tutorial code closely since it sounds like your problem maps well onto the sample they provide.

When should a Service be stopped

My android application starts a service in the onCreate() callback of a class that extends Application. The service performs some background tasks that are relevant to the user only while the application is running. For that reason I would like to close the service when the application's last activity is closed. I've tried to perform closing the service in the callback onTerminate() , but it never gets called . So what would be the best place where a service should be closed ?
Thanks !
An Android service, once started, will continue running until the Context.stopService() or stopSelf() is called.
There are various hooks you can use to stop the service using Context.stopService (the service itself, or an onDestroy()/onPause callback in one of the activities, or a button click).
It's true that Android does some resource management itself, but it can take a long time before Android decides to terminate your services. And a service that's running but not doing anything just consumes resources on the phone that other apps might need.
In your case, the onPause method of your last activity would be a good that will get called, and as such is the correct place to stop the service.
The onPause() callback will be made when your activity is paused for any reason, and you know that when this happens your app will not be visible again until onResume() is called. If your service has a reason to run in the use case that your activity might be started again soon, you should add an entry to your service that onPause() calls, to set a delayed service termination. In onResume() you can cancel that delayed termination through another entry.

Categories

Resources