2

I have the below piece of code.

    Fragment emptyViewFragment;
    Fragment songListingFragment;

    // .... and various codes ...

    if (emptyViewFragment == null) {
        emptyViewFragment =
                (EmptyViewFragment) getFragmentManager().findFragmentByTag(EmptyViewFragment.TAG);
        if (emptyViewFragment == null) {
            emptyViewFragment = new EmptyViewFragment();
        }
    }
    addFragment(emptyViewFragment, EmptyViewFragment.TAG);

    if (songListingFragment == null) {
        songListingFragment =
                (SongListingFragment) getFragmentManager().findFragmentByTag(SongListingFragment.TAG);
        if (songListingFragment == null) {
            songListingFragment = new SongListingFragment();
        }
    }
    addFragment(songListingFragment, SongListingFragment.TAG);

The two pieces of if-else codes looks very similar, and I'm thinking of making a function from it, so that I could just make my code as as below: -

    EmptyViewFragment emptyViewFragment;
    SongListingFragment songListingFragment;

    // .... and various codes ...

    createFragment(emptyViewFragment, EmptyViewFragment);
    createFragment(songListingFragment, SongListingFragment);

or even better (since the 2nd parameter is the 1st parameter pass in object's type).

    EmptyViewFragment emptyViewFragment;
    SongListingFragment songListingFragment;

    // .... and various codes ...

    createFragment(emptyViewFragment);
    createFragment(songListingFragment);

How should my createFragment function be written?

1
  • Which IDE are you using? If it is Eclipse, you can use the short-cut (Alt+Shift+M) to generate a common method and later you can put additional Class parameter accordingly. Commented Nov 20, 2015 at 10:14

4 Answers 4

3

Maybe something like this

private void a (Fragment frag, Class<? extends Fragment> clazz, String tag) {
    if (frag == null) {
        frag = getFragmentManager().findFragmentByTag(tag);
        if (frag == null) {
            try {
                frag = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    addFragment(frag);
}

You can access to the tag attribute inside and delete the tag parameter too

UPDATE

I can not test it now, but I think you can access to the static member TAG in this way

private void a (Fragment frag, Class<? extends Fragment> clazz) {

    try {
        if (frag == null) {

            frag = getFragmentManager().findFragmentByTag(clazz.getField("TAG").get(null).toString());

            if (frag == null) {
                frag = clazz.newInstance();
            }
        }
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    addFragment(frag);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Francisco! Can I not send the TAG in, but access through the class or the object? The TAG is a final static string in the class.
frag.getClass() will throw NPE, since frag is null at this point.
Yessss. you are right. Pass the class instead, I will update the answer
Thanks Francisco, I finally ended using your first approach above, sending in the TAG directly. Tick for your answer.
2

Instead of using the class, you can use an abstract factory:

Fragment createFragment(Fragment fragment, FragmentFactory factory) {
    if (fragment == null) {
        fragment = getFragmentManager().findFragmentByTag(factory.getTag());
        if(fragment == null) {
            fragment = factory.createFragment();
        }
    }
    addFragment(fragment, factory.getTag());
}

Where FragmentFactory is an interface which will have one implementation for each fragment type.

Comments

1

Try something like this:

public addFragment(Fragment frag) throws IllegalAccessException, InstantiationException {
    Fragment newFrag = frag.getClass().newInstance();
    //...
}

This will not work if frag == null.

If this is your use case, then get the class by using Class<Fragment> clz = EmptyViewFragment.class, then call clz.newInstance().

1 Comment

Thanks dotvav! How should I handle the TAG? TAG is a static final string variable in the class.
0
/**
 * @param fragment
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
private static void createFragment(Fragment fragment, String tag, Class<? extends Fragment> clazz) throws InstantiationException, IllegalAccessException {
    if (fragment == null) {
        fragment =
                 getFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            fragment = clazz.newInstance();
        }
    }
    addFragment(fragment, tag);
}

Assuming that Fragment is a Parent class of other to *Fragment classes and with following method defintion :

    /**
 * @return
 */
private static Fragment getFragmentManager() {
    // TODO Auto-generated method stub
    return null;
}

1 Comment

Hi Dishi, I think fragment.getClass().newInstance(); will crash, given fragment is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.