1
\$\begingroup\$

I'm new to using fragments in Android. I'm working on an app using a sliding panel layout and I just came up with an idea for changing my fragments. I decided to create the following method:

 private void changeFrag(Object newFrag){
 // Insert the fragment by replacing any existing fragment
 FragmentManager fragmentManager = getSupportFragmentManager();
 fragmentManager.beginTransaction()
 .replace(R.id.fragment_secondpane, (Fragment) newFrag).commit();
 }

I'm calling the method in the following way:

 changeFrag(new myFragment());

Could this cause problems for me in the long run? I was hoping that by writing it this way, I could avoid extra code and hopefully cut down on any extra processing power the device would need for something like this:

 Fragment frag;
 frag= new myFragment();
 // Insert the fragment by replacing any existing fragment
 FragmentManager fragmentManager = getSupportFragmentManager();
 fragmentManager.beginTransaction()
 .replace(R.id.fragment_secondpane, (Fragment) frag).commit();
asked Oct 4, 2013 at 15:07
\$\endgroup\$
6
  • \$\begingroup\$ have you tested this? does it work? \$\endgroup\$ Commented Oct 4, 2013 at 17:20
  • 1
    \$\begingroup\$ Sorry Forgot mention that. Yes is seems to work. \$\endgroup\$ Commented Oct 4, 2013 at 18:20
  • 2
    \$\begingroup\$ Why did you declare newFrag as Object, not Fragment? \$\endgroup\$ Commented Oct 5, 2013 at 11:22
  • \$\begingroup\$ And where is frag defined? Is it newFrag in the first block and fragment in the second? If so, both blocks look identical save for putting the code into a method. \$\endgroup\$ Commented Oct 6, 2013 at 15:17
  • \$\begingroup\$ @David Harkness Thank you for spotting the coding errors I made when I change made the variable names generic. \$\endgroup\$ Commented Oct 7, 2013 at 13:46

1 Answer 1

2
\$\begingroup\$

You could make it more dynamic. Because this method adds the Fragment only to the R.id.fragment_secondpane layout. If this is all what you desire, it's ok. Otherwise add a parameter for the layout. This way you can use your method to add a Fragment to different layouts.

Because every type of Fragment like ListFragment, DialogFragment or even a SherlockFragment is using Fragment as super class, you can use Fragment as the parameter type. There's no need to use Object. Note that you should avoid to mix the Fragment types of different librarys.

With methods like this you can avoid extra code in this situation, replacing different Fragments of a specific layout.

The processing will be the same. The creation of the necessary objects have to be done. There will be no difference if this happens in e.g. onCreate or the method you wrote. The only thing you could avoid is to hold an extra reference of the FragmentManager.

To sum all up you would have something like this:

private void changeFrag(int layout, Fragment newFrag){
 getSupportFragmentManager() // gets the FragmentManager of the Activity, no need to hold a reference
 .beginTransaction()
 .replace(layout, newFrag) // use the layout and the fragment, no need for a cast
 .commit();
}

Note that this method will work and is straightforward. If you have an approach with adding some Fragments to the backstack and some not or using different kind of animations, your method will be more complex and lacks performance. That's why I would not use this in every situation.

answered Oct 6, 2013 at 13:19
\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much for your input I appreciate everyone's help on this! \$\endgroup\$ Commented Oct 7, 2013 at 13:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.