0

I'm reading article "Managing the Activity Lifecycle" (http://developer.android.com/training/basics/activity-lifecycle/index.html) and I'm little confused. Maybe first I'll try to say what my application do. So, this is some kind of http client. User login to the server and client store authorization (session id). After login, TimerTask is executing, which for every 10 seconds get some small json from server and by the way server know that authorization key is still alive (normally it is valid for ~30 minutes). In this json could be some events which should be shown to the user (I'm using notification manager for this) or questions for which user should answer (I'm showing custom dialog with "Yes", "No", "Don't know" and then POST answer to the server).

This works fine when my application is in foreground, but I don't really know what should I do if the application is stopped/paused.

My doubts:

  • I want that TimerTask should works even if user click home button or another application appear. There are two reasons: One - user need to be notified about events, two - I need to keep alive session id. But this article say that when activity is in foreground, it should release resources. What this mean? What are restrictions? Must I stop my timer?
  • Documentation say that system can kill application when it is no longer needed. What does it mean no longer needed? When user don't use it or when application code do nothing for a while? Can my TimerTask keep alive application?
  • Storing authorization key. I need remember session id in situations where activity is recreated by system like orientation change, etc. I'm using for this SharedPreferences object. Problem is that using this object, key is saved in database and I can't recognize when my application is closed permanently (which mean "logout") or just recreated because orientation is changed. This occur situations when user run application again after couple hours and my activity restore dead session id (my application look like "logged in" because authorization variable is not empty and I use this state as a flag). I need some temporary version of SharedPreferences object. What about bundle object passed in onSaveInstanceState? Is it temporary?

Regards

Dibo
  • 1,159
  • 17
  • 34
  • My impression is that you should use a service for handling the connection to your server and intents/foreground activity to interact with the user. The service should be persistent and first stopped when the user decides that the application should be closed. – wojciii Jul 19 '12 at 10:59
  • Hm, service sounds good. I'm reading about this right now. But how service affect to application lifecycle? I'm mean, if my application has service then android will don't kill my foreground application until user finish it manually? (this is what I want to get). Can I use service in this same process as activity (in my case)? In short: I must create activity which start service and this service start my TimerTask? – Dibo Jul 19 '12 at 13:57
  • The system can kill your activity at any time. If you use a service, your activity can be killed, but your service will continue to run and can for example inform the user about events. So yeah, the service should have a timer and connection to your server and the activity should have a connection (using for example AIDL or [Messenger](http://developer.android.com/reference/android/os/Messenger.html) instance). If the user quits the activity you can still use notifications to tell him about events. – wojciii Jul 19 '12 at 15:47
  • I did some test with prototype of my service and works fine :) . But I'm confused how to properly start and stop service. When user run application first time, activity create service by startService(). Should I somewhere stop this service to avoid memory leaks? I have "Finish" button on activity which stop service and close whole application (Activity.this.finish) but is it enough? And what in situations where system kill activity and recreate it again? Activity create second instance of this same service? – Dibo Jul 19 '12 at 17:51
  • When you are done using your service you can instruct it to terminate itself and thus free the memory it used. – wojciii Jul 19 '12 at 18:13
  • But if user try to finish aplication from android running applications list, then service is stopped automaticaly? I'm using service in this same process as activity – Dibo Jul 19 '12 at 18:26
  • Please see this [entry](http://stackoverflow.com/questions/9458300/how-to-create-an-android-activity-and-service-that-use-separate-processes). – wojciii Jul 20 '12 at 06:25

1 Answers1

0

It looks like you are on the right track. I think that you should continue reading documentation. Some suggestions:

wojciii
  • 4,253
  • 1
  • 30
  • 39