I'm also using multiple fragments, each one will do a different task through the UI. As the user works with the data the app will need to be able access data in the model layer across fragments.
I initially spent several hours looking for approaches to pass data between fragments. There are ways I found to do this, but they all seemed clumsy or looked like they would be time consuming in terms of copying data. I should have been paying more attention to my pattern all along.
-- An aside to plug a good book! --
BTW!! - A big nod goes to the Bill Phillips and Brian Hardy and their book "Android Programming - The Big Nerd Ranch Guide".
This solution is cut whole cloth from their book with just a few flavors I've added... This is one of the best Android programming books I've found for someone like me who stumbling through the process of learning this stuff! They have a project you can work with, but they also explain what's going on in the background. I'm able to use their book as a reference while I write my own applications... That's a big step up from some of the others I've used, which only give you snippets of capabilities and hints about what might be going on.
-- And now back to this discussion...
The basic approach to make this work will be:
- First, hide the data well in the Model layer. How will you access this data from different fragments? Their will be classes in the model layer which act as object managers. These object managers will have a get(Activity) method which returns a pointer to themselves (or creates themselves if they have not been instantiated yet). Then any fragment which gains a pointer to these object manager classes can use the object managers to get the actual data, modify it, and restore it. This is a much better method than trying to pass copies of objects around!
- Create a class with an abstract method for the activities in the app. Each Activity will be forced to create a fragment, and will provide the container ID from the XML layout files. The activity class will then create the fragment. Each fragment is now created under an activity.
- We can now use the intents, bundles, and ability of activities and fragments to send information back and forth about what should be gathered out of the model layer as well as notify the initiators of fragments that work is finished and they can update their UI displays.
So, with that brief explanation.. Here are a few snippets of code from Nationals...
Hide the Model Layer
In my app there are two classes which contain most of the data. There is a class "Region" which contains information about the state of different parts of Europe. There is another class called "Railroad" which contains information about the state of different railroad corporations.
The user will want to update the Region objects as players modify track and improve value of cities.
The user will also want to update the current Railroad objects so they have correct information about how many trains and what kind of trains the Railroads have.
The main screen will use these underlying objects to display the current revenue for each railroad.
To "Hide the Model Layer" I'll use the BigNerdRanch (BNR's) approach of putting an intermediate "Holder class" in place to hold references to regions, and another to hold references to the trains.
How does an activity get the actual pointer to an object in memory from this "Holder Class"?
The holder class will have a static member that is the reference to itself. It will also have a static method to return this static pointer. Any fragment can call this static method to get the pointer to the holder class. Once it has the holder class it's connected to the rest of the model view!
Here's the get method for "Europe", which is the holder class for all the Regions.
public static Europe get(Context c){
// europe is the static member holding
// the reference to this holder object.
if (europe == null){
europe = new Europe(c.getApplicationContext());
}
return europe;
}
I'll try to take time to document the abstract class for creating fragments tomorrow or the next day, but this is enough for one entry.
No comments:
Post a Comment