That approach sounds good to our docs team. - Jon
On Oct 29, 2012, at 2:43 PM, Goncalo Oliveira <gonc...@minkan.net> wrote: > Hi all, > > I'm having this dilemma on Activity/Service "binding" that I don't quite know > what is the best way to achieve. > Here's a common scenario... > > We have an activity named LoginActivity. This activity has two fields, > username and password. > The authentication operation consists in making a web request, and processing > the result. Visually, we want to display a nice progress bar while we wait. > > That's it. Pretty common. > Obviously, we want to consider things like the activity getting paused or the > screen orientation changing. > > After reading the docs and going through a few forums, I came up with a > solution. > > First, the dialog issue. If the screen orientation changes, the view is > destroyed and recreated, so the dialog just disappears (and worse, it's lost > in space - leaks). > What I did was to keep a reference to the progress dialog shown. I retain a > flag stating that the progress dialog is visible by overriding the > OnRetainNonConfigurationInstance. I also make sure I dismiss the dialog when > the activity gets trashed (destroyed). When the new view gets created, I > recover the flag and show a new progress dialog. This works well. > There were other methods, like configChanges, but I didn't want to handle the > view resources myself, so this seemed like a good and clean approach. > > Now for the operation, I created a service to handle it. More specifically an > IntentService. The request is done in background, and when done, I send a > broadcast with the result. Because the activity might pause (or trashed), I > send a sticky intent broadcast, to make sure it reaches its destination. When > received, I clear up the sticky intent. > > This works quite well... > Though I was happy with the result, I questioned if this was the best > approach. Some defend that using sticky intents is evil, even though I do a > very assertive use. Some say that service binding is a better approach, > though I don't know if it can be used with an IntentService and it seems like > an awful lot of trouble for such a simple task (maybe I'm just not looking > correctly). > > So, any thoughts on this approach and on best practices for accomplishing > this are greatly appreciated. > Cheers. > > > -- > Gonçalo Oliveira > _______________________________________________ > Monodroid mailing list > Monodroid@lists.ximian.com > > UNSUBSCRIBE INFORMATION: > http://lists.ximian.com/mailman/listinfo/monodroid _______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid