1

Im trying to use the pattern of Activity-Service-Messenger to comunicate my Activity and Service. (like explained here http://viktorbresan.blogspot.mx/2012/09/intentservice-and-inter-process.html) Basically it says that i should create a Handler inside my Activity, and then create a Messenger and send that via putExtra() to my Service. The Service would then post messages to the activity ussing the Messenger.

My problem is that if i rotate the emulator, the Handler associated with the Messenger holds a reference to a destroyed activity. This causes not to refresh the interface of the new activity. I tried to put Messenger in onSaveInstanceState(). Eventought i can save the Messenger, the Handler is still referencing my past activity and i cant find a way to retrieve my Handler from the Messenger to set the new activity.

Edit:
Im avoiding to using:
android:configChanges="orientation|keyboardHidden"
onRetainNonConfigurationInstance()

Edit:
I used HalR idea of using a singleton and keep the handler there. It works really good, althought i can see that this pattern implies a careful cleaning of the references on the singleton.
Finally im also testing on the idea of using Activity-Service that was commented by Hoan Nguyen

user1546652
  • 557
  • 1
  • 5
  • 15
  • Is your service an intent service? – Hoan Nguyen May 12 '13 at 16:43
  • @Hoan Nguyen: yes dude. Ive been testing different ways to communicate IntentService and Activity. ive tried activity-service-broadcast pattern, but i was concerned that the signal arrived when i unregisterReceiver(). Now i was experimenting with activity-service-messenger-singleton with HalR idea of using Singleton, it changed my paradigm totally. I think its very helpful the use of a singleton, and in fact im thinking about the idea of an activity-service-broadcast-singleton pattern so i can solve the problem of unregisterReceiver() that i was concerned at first. – user1546652 May 12 '13 at 17:11
  • 1
    Why don't you use regular service instead of intent service? – Hoan Nguyen May 12 '13 at 17:15
  • That was my next test. Try to use a Service instead of a IntentService. I begin with IntentService because they are easier but had the problems i posted. Do you have a link for a good example of Service and Activity communication? Thanks again! – user1546652 May 12 '13 at 17:20
  • In the service document there is some example codes. http://developer.android.com/reference/android/app/Service.html – Hoan Nguyen May 12 '13 at 18:01

2 Answers2

1

I'm not sure that its appropriate for this case, but there are many people who have been frustrated by losing their activity when it rotates, or having to set complex stuff up every time they get a new activity.

Some people will create singletons that they use for referencing, then keep the Handler in there.

Others will extend the application class and put stuff in there. If you have a lot of complex things you are wanting to set up once, those are techniques you can use.

Keeping your app fluid and your making your activities independent of one another is a better overall philosophy, so its best to avoid anything global, but sometimes you gotta do what you gotta do.

Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
  • 1
    Dude, your answer works like a charm. I made a Singleton and inside ive a reference to my Handler which i can use to set the activity in foreground. In particular Im thinking that this pattern Activity-Service-Messenger-Singleton is better that Activity-Service-BroadcastReceiver for Service and Activity communication. If no more answers are thrown ill stay with yours as the correct. Thanks again! – user1546652 May 12 '13 at 16:43
0

Rotating the device at least pauses and resumes your activity according to the lifecycle. I think you are aware of the consequences. Maybe stopping and starting a new service is the only right solution here. i worked as well with global states, but it will just always be easier when, you make every activity independent like a "single application".

edit: ok it's a messenger service... so stopping and starting is not a solution. so maybe you can register and unregister your messengers.

Diego Frehner
  • 2,396
  • 1
  • 28
  • 35