-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Consider this example of activities from my application:
0. Activity 0: render the list of bird observations 1. Activity A: pick a bird species from a list (or in more sophisticated ways, browsing through a taxonomy and drilling down filtered results) 2. Activity B: insert the count and the gender of the watched birds 3. Activity C: confirm or edit the name of the current place 4. Activity D: show a fact sheet about a bird species At http://bluebill.tidalwave.it/mobile/features/ there's a semi-decent screencast of activities 0, A, B, C. Now, starting from Activity 0, two commands start two sequences of operations: 0 -> A -> B -> C -> add the observation to the model -> 0 0 -> A -> D My point is that, according to the Android best practices, activities must be designed so they can be as much self-containing as possible and have the least, or even zero, dependencies on other Activities. Still, to realize a concrete sequence of operations for the user, you might need to assemble more activities together as in my example and, note again in the example, the same activity A could be reused in different sequences. The responsibility of A is just "pick a bird species" and should not know neither who called it, nor whom has to be executed at the next step. I could even reuse it in another way, not starting from 0, for instance after a photo has been shoot: photocamera activity -> shoot a photo -> pop up a dialog box asking whether you shooted at a bird -> A -> record the photo as an attachment of an observation -> photocamera activity In a similar fashion, activity C could be reused in any other application that just needs to enter the current location name. Of course, Android designed Activities so that they communicate by message-passing, through Intents. Intents both support topic-based and point-to-point message passing, but this is not enough as the decision of going from A to B or from A to D must be context-depending. So far, I've wired things down in my app with some explicit 'if' and it's not good. I've figured out two strategies, so far, for constructing sequences of activities in a good fashion: 1. using a centralized controller. In a word, all the activities always call finish() when they are completed; activity 0 acts as a controller state machine, e.g. when it detects that A has been completed, it starts B, then C after B etc (I'm calling of a state machine because, of course, you might have rules and branches in the flow). It works, but the problem is that this doesn't appear to Android as a true sequence: I mean, hitting "back" where you are in B just returns to 0, and in order to return to A needs explicit back handling code in the state machine. Furthermore, if you have activated the slide in animation for activities, the slide-from-left / slide-from-right convention that should distinguish among going forward, backward and completing is not working (and you have again to patch things in code). I mean, it's doable, but it's wasting all the built-in support for navigation that Android provides, so it doesn't look as it's a best practice. 2. Embed the state machine in a Command pattern that, in turn, is embedded in the Intent. That is, whenever an Activity completes, it doesn't start any new activity nor calls finish(), but delegates the decision to the Command. In this way, I could launch A with a Command saying to continue with B, or with D, and be able to reuse A in every sequence I need. Since Command would just call startActivity() or finish() when appropriate, the flow appears as a proper sequence to Android, and I have history and back behaviour working out of the box. Of course I prefer much #2. But I'd like really to avoid reinventing the wheel and perhaps there are even better approaches. Maybe I've googled with the wrong keywords, but I've been unable to find a comprehensive document describing the problem and the strategies to solve it. Am I missing something? Maybe there's some good example in the code samples, but I haven't looked at all of them, so a pointer would be appreciated. Thanks. - -- Fabrizio Giudici - Java Architect, Project Manager Tidalwave s.a.s. - "We make Java work. Everywhere." java.net/blog/fabriziogiudici - www.tidalwave.it/people [email protected] -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkvhQHIACgkQeDweFqgUGxdVBgCgoNxJ/GF2WraDPOX6bqNbkcon Df4AniEdYQXjg6Ly5WK8eE4N1qkV3BKO =S/eh -----END PGP SIGNATURE----- -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
