-1

I am writing an android application. In the application there are 4 activities.All the activities are mutually independent.


In my application i want to use a socket communication and a service. Both of them starts when the first activity is launched and should be stopped when the last activity exits. The starting activity is preset, so no problem in the starting. But the last activity is random. In this situation how should i stop the service and close the socket?? Is there any callback when the "Application" exits? I have seen the android activity life cycle, But it doesn't says anything about the whole process....

jsaji
  • 900
  • 1
  • 15
  • 31

2 Answers2

0

How about using AIDL-interface to a service and let each activity register/unregister. Then let the service count number of activities active and when the counter reaches zero close the socket and exit.

mach
  • 8,315
  • 3
  • 33
  • 51
0

onCreate and onDestroy - bracket the entire life of the app. This pair is called when the app is loaded into memory or unloaded from memory. These two bracket the entire lifetime of an activity. When it is first loaded the onCreate is triggered and when the app is disposed of onDestroy is triggered. You clearly have to use these two to setup and destroy and resources which are needed for the entire lifetime of the app - although in practice things can be more subtle. The system can stop the app without calling the onDestroy and can restart the app triggering an onCreate event.

onStart and onStop - bracket any period that the app is visible. It could be that the app is behind say a modal dialog box. The app is visible but not interacting with the user. This pair of events can be triggered multiple times during the entire lifetime of the app. Simple apps can mostly ignore the onStart and onStop events because the app is still in memory and doesn't loose any resources or state. The main use of onStart and onStop is to give the app an opportunity to monitor any changes that might affect it while not interacting with the user. To confuse the issue even more there is also on onRestart event which occurs before the onStart event but only if this isn't the first time the app has fired the onStart - that is this is a true restart.

onResume and onPause - bracket the period that the app is in the foreground and interacting with the user. Again this pair of events can happen multiple times during the entire lifetime. The onResume event occurs when the app is in the foreground and doing its usual job. The onPause event occurs when the user switches away to another app for example.

You can learn a lot about lifecycles in this Adventure: Have a look at it: http://www.i-programmer.info/programming/android/5966-android-adventures-lifecycle-and-state.html

Edit:

Maybe this will help you: How to handle activity life cycle involving sockets in Android?

And here is a good guide on how to use them: http://tacticalnuclearstrike.com/2011/03/a-way-of-using-sockets-in-android/

Community
  • 1
  • 1
DominikAngerer
  • 6,354
  • 5
  • 33
  • 60
  • onDestroy not always be called – Suvitruf - Andrei Apanasik May 12 '14 at 11:32
  • Sorry - read over it - I made an Edit with an question which got an answer on "how to handle sockets in android" You always have a MainActivity and there you can override onDestroy – DominikAngerer May 12 '14 at 11:47
  • @Domi This is what i have done... The problem is that when ever the activity changes the socket is created and destroyed. I want to make it single open and single close for the entire application.. – jsaji May 12 '14 at 12:13
  • You can check with the function isFinishing() if your application is finishing or not - so there you can destroy your sockets. http://stackoverflow.com/questions/5227071/understanding-of-isfinishing – DominikAngerer May 12 '14 at 12:17