3

I have an application whose Activity's purpose is to connect to a server, keep and monitor the connectivty and on disconnection inform the user about it. Activity has other dialogs and its UI and also a service running while the connecting and monitoring connectiivty. Service starts and stops from te UI thread only.

My query : once connection is established the service will be started and activity will be hidden. From lifecycle I can understand on hidding it will go to the onPause() stage. And then if device needs memory it can kill my activity. If the connection is not established I can let it be killed, but if established, then I don't want it to be killed without user's knowledge.

How to hide the Activity ? How can I stop it from being killed ? OR if it is being killed how to inform user for the same. If activity is killed what will be the condition of service. I don't think tha will also be killed. That becomes mismatch on restart of application.

EDIT INFO : Thanks @indyfromoz and Aleadam, @Aleadam, I had a look at all those links and that helped me understand and develop my service nicely. I do bind the servie with the Activity and the service has stub that is returned to the Activity, And as required service sends message to the Actiivty - on some msgs only activity will inform the user but messages are notified by the service to the activity. Secondly another AsyncTask is also running which will also notify activity on regular intervals. And if any connection is lost, Activity handles to start that task/service again informing the user. I have managed this much so far, but am just concerned about the killing of activity. Service and ASyncTask will be running and notifing user, but UI wont be visible all time on top, so that will be in onPause stage. If in low memory case system kills the activity then ?

Do you try to say that if service is binded and notification will be going on then activity wont be killed as it is active though not visible ? I am little bit confused, concerned and don't understand clearly is their any req to handle this stage of my application. If so, how ? For better understanding have given my flow of the app below.

Flow : Activity start Some input, validation, UI, etc Start & Bind Service - startService(bindIntent); bindService(bindIntent, sevConn, flag); Service started/bounded successfuly ? start AstncTask : Re-Try & inform (re-try limits)

AsyncTask Started NOW UI IS NOT REQD TO BE ON TOP, BACKGROUND WORK IS GOING ON

Both AsyncTask & Service gives Notifications that are handled by Handlers in Activity. On any sp msg display to user and maybe show UI back.

Thanks

Tvd
  • 4,463
  • 18
  • 79
  • 125

1 Answers1

1

The Service could check to see if the Activity is running (via an IBinder) and start a new one in case it was killed see my response here for links to tutorials on this.

Another possibility is to create an Application object that has references to both the Activity and the Service, that you can use to manage your needs. Applications are less likely to be killed than Activities. Anyhow, it all comes down to restart your Activity if it was killed. Use saveInstance() to save any data you need from your Activity (See this question)

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • Aleadam, Thanks for the links. Kindly have a look at the indo added to question. Do you say that using IBinder, and binding it with the activity, untill the service is running, activity wont go in pause stage for longer i.e. what I am concered is somewhat wrong ??? – Tvd May 16 '11 at 08:53