Useful design patterns for working with FragmentManger on Android

Posted by antman8969 on Programmers See other posts from Programmers or by antman8969
Published on 2012-12-11T04:27:00Z Indexed on 2012/12/11 5:18 UTC
Read the original article Hit count: 229

Filed under:
|

When working with fragments, I have been using a class composed of static methods that define actions on fragments. For any given project, I might have a class called FragmentActions, which contains methods similar to the following:

public static void showDeviceFragment(FragmentManager man){
    String tag = AllDevicesFragment.getFragmentTag();

    AllDevicesFragment fragment = (AllDevicesFragment)man.findFragmentByTag(tag);

    if(fragment == null){
        fragment = new AllDevicesFragment();
    }

    FragmentTransaction t = man.beginTransaction();
    t.add(R.id.main_frame, fragment, tag);

    t.commit();
}

I'll usually have one method per application screen. I do something like this when I work with small local databases (usually SQLite) so I applied it to fragments, which seem to have a similar workflow; I'm not married to it though.

How have you organized your applications to interface with the Fragments API, and what (if any) design patterns do you think apply do this?

© Programmers or respective owner

Related posts about design-patterns

Related posts about android