The second part of making the MVC model work cleanly with an underlying data model is to create the view and controller levels easily.
Currently if you create a new project in Eclipse you'll get an Activity connected to what is essentially an empty view (OK, there's a simply layout which is called 'container', but it's empty). The Activity created by the SDK doesn't do anything except start a Fragment that is placed inside the container and brings along its own xml layout for the actual view that your app will work it.
I think there might be a hint there from the folks at Google regarding their feelings about using Fragments...
The guys at Big Nerd Ranch (BNR) take this one step further. They recommend creating a class they call "SingleFragmentActivity". They observe quite correctly that you're basically going to do two very simple things with your activities now that you're "hooked on fragments."
The class is exactly like the one created for you by default with one exception, it comes with a abstract class that will be implemented by later classes which extend this one.
Since the shell activity really doesn't do anything, the only really interesting bit here is the Fragment you're going to start, and that's the only thing that morphs from instance to instance as well.
BNR delegate that bit to the abstract class. I've implemented this in a couple of ways. Once with graphic fragments and once with widget/XML based fragments.
Here's the magic (once again I'm noting that a lot of this is from the BNR book with just a few extras of my own... )
First, create a superclass that will have abstract methods in it. This class will extend FragmentActivity (an android class to create an activity that wants to use support for fragment and API loaders.
public abstract class genericFragmentActivity extends FragmentActivity {
...
I think there might be a hint there from the folks at Google regarding their feelings about using Fragments...
The guys at Big Nerd Ranch (BNR) take this one step further. They recommend creating a class they call "SingleFragmentActivity". They observe quite correctly that you're basically going to do two very simple things with your activities now that you're "hooked on fragments."
- Connect the Activity to a layout that has a container for the fragment
- Start (or restart if it exists already) the fragment in the container.
The class is exactly like the one created for you by default with one exception, it comes with a abstract class that will be implemented by later classes which extend this one.
Since the shell activity really doesn't do anything, the only really interesting bit here is the Fragment you're going to start, and that's the only thing that morphs from instance to instance as well.
BNR delegate that bit to the abstract class. I've implemented this in a couple of ways. Once with graphic fragments and once with widget/XML based fragments.
Here's the magic (once again I'm noting that a lot of this is from the BNR book with just a few extras of my own... )
First, create a superclass that will have abstract methods in it. This class will extend FragmentActivity (an android class to create an activity that wants to use support for fragment and API loaders.
public abstract class genericFragmentActivity extends FragmentActivity {
...
Normally when you're going to have a shell Activity that will start a Fragment you'll simply do the work in the "OnCreate() Method, and that's the case here as well. For this class this is all we'll do. We'll let any future implementation set up menus and the like in the child class. But here's the OnCreate()Method...
@Override // this method is called by Android during creation of activities
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity); // this is the empty xml layout
// define the container that will be used to find or create the fragment
setFragmentContainerID(); // (see below in blog!)
// the supportFragmentManager is provided by the OS... helps us start it. FragmentManager manager = getSupportFragmentManager();
// use the manager to get the fragment reference, if it already exists.. Fragment fragment = manager.findFragmentById(fragmentContainerID);
// if the fragment exists we have it already.. if not? Create it! if (fragment == null){
fragment = createFragment();
// the fragmentContainerID is defined by the child class (below!)
@Override // this method is called by Android during creation of activities
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity); // this is the empty xml layout
// define the container that will be used to find or create the fragment
setFragmentContainerID(); // (see below in blog!)
// the supportFragmentManager is provided by the OS... helps us start it. FragmentManager manager = getSupportFragmentManager();
// use the manager to get the fragment reference, if it already exists.. Fragment fragment = manager.findFragmentById(fragmentContainerID);
// if the fragment exists we have it already.. if not? Create it! if (fragment == null){
fragment = createFragment();
// the fragmentContainerID is defined by the child class (below!)
manager.beginTransaction().add(fragmentContainerID,
fragment).commit();
}
}
fragment).commit();
}
}
This is all you see in the abstract class except the method by the BNR guys and my addition.
The BNR guys create an abstract method that will create the fragment. It's simply:
protected abstract Fragment createFragment();
When the child class implements the abstract method they simply have to instantiate the fragment class and return the reference to the fragment. The rest is taken care of above.
As soon as I started using this I realized that not all of my fragments will go in the same container widget. As a result I wanted to be able to modify the identifier that points to the container widget. Initially I simply created a member for this, but then I realized that it's very easy to forget to define the widget (when my code crashed after I forgot!). So I added an abstract method to force the child class to define it.
protected int fragmentContainerID; // ID of xml widget that hosts this fragment
// this second abstract class is only really here to force the
// child class to define the fragmentContainerID - it's easy to forget otherwise.
protected abstract void setFragmentContainerID();
And that's it. Now there's a simple structure in place to create Activities that immediately and painlessly kick of Fragments. Using this model you'll be able to use the bundles and result features of Activities, and still use the fragments that are so flexible with respect to the UI...
Here's my Activity class to end with, just to show you how simple these are now. This one is the activity that will host the fragment to edit the Regional classes.. I'm using the intent feature to send the name of the regional object to the Fragment so it can fetch from the model.
package moticon.nationals;
import android.support.v4.app.Fragment;
public class RegionalEditActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
String regionName = (String)
getIntent().getSerializableExtra(NationalConstants.REGION_ID);
return RegionalEditFragment.newInstance(regionName);
}
@Override
protected void setFragmentContainerID() {
this.fragmentContainerID = R.id.container;
}
}
The BNR guys create an abstract method that will create the fragment. It's simply:
protected abstract Fragment createFragment();
When the child class implements the abstract method they simply have to instantiate the fragment class and return the reference to the fragment. The rest is taken care of above.
As soon as I started using this I realized that not all of my fragments will go in the same container widget. As a result I wanted to be able to modify the identifier that points to the container widget. Initially I simply created a member for this, but then I realized that it's very easy to forget to define the widget (when my code crashed after I forgot!). So I added an abstract method to force the child class to define it.
protected int fragmentContainerID; // ID of xml widget that hosts this fragment
// this second abstract class is only really here to force the
// child class to define the fragmentContainerID - it's easy to forget otherwise.
protected abstract void setFragmentContainerID();
And that's it. Now there's a simple structure in place to create Activities that immediately and painlessly kick of Fragments. Using this model you'll be able to use the bundles and result features of Activities, and still use the fragments that are so flexible with respect to the UI...
Here's my Activity class to end with, just to show you how simple these are now. This one is the activity that will host the fragment to edit the Regional classes.. I'm using the intent feature to send the name of the regional object to the Fragment so it can fetch from the model.
package moticon.nationals;
import android.support.v4.app.Fragment;
public class RegionalEditActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
String regionName = (String)
getIntent().getSerializableExtra(NationalConstants.REGION_ID);
return RegionalEditFragment.newInstance(regionName);
}
@Override
protected void setFragmentContainerID() {
this.fragmentContainerID = R.id.container;
}
}
No comments:
Post a Comment