23

General Question Can I define Fragments as Singletons?

Specific question In my application I have one 'FragmentActivity' with a FragmentPager which has two Fragments, FragmentA and FragmentB.

I defined the fragments as Singletons in the FragmentA extends Fragment class:

private static instance = null;

public static FragmentA getInstance() {
    if (instance == null) {
        instance = new FragmentA();
    }   
    return instance;
}
private FragmentA() {}

and in my FragmentPagerAdapter :

@Override
public Fragment getItem(int position) {
    switch(position){
    Fragment fragment = null;
    case 0:
        fragment = FragmentA.getInstance(); 
        break;
    case 1:
        fragment = FragmentB.getInstance(); 
        break;
    }
    return fragment;
}

and this is how I inflate the fragments layout:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    fragmentView = (RelativeLayout) inflater.inflate(R.layout.fragment_a_layout, container, false);
    return fragmentView;
}

My Problem:

When I first launch my app everything works well. When I close my app and then restart it, I'm not seeing both of the fragments.

Daniel L.
  • 5,060
  • 10
  • 36
  • 59

1 Answers1

30

Fragments are meant to be reusable components of applications. You should not be using them as singletons, instead you should implement Fragment.SavedState or onSavedInstanceState.

public class YourFragment extends Fragment {
    // Blah blah blah you have a lot of other code in this fragment
    // but here is how to save state
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }
    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // savedInstanceState will have whatever you left in the outState bundle above
    }
}
hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
  • I originally wanted to do that because my fragments are always visible, and I support only portrait orientation. – Daniel L. Feb 12 '13 at 18:31
  • Still I cant understand why when I close my and restart it it acts differently. Does fragments kept alive when my application is closed? – Daniel L. Feb 12 '13 at 18:59
  • 1
    Fragments can be kept alive when your activity is closed, but they can also be reclaimed if the system needs memory. Fragments are not guaranteed to be around all the time. – hwrdprkns Feb 12 '13 at 19:02
  • Thanks, so if I get it right, I better off just using regular View Pager with my custom views instead of Fragments? – Daniel L. Feb 12 '13 at 21:06
  • No, you're better off using Fragments. The ViewPager will get reclaimed in the same manner. Fragments are more robust, sort of like ViewControllers. – hwrdprkns Feb 12 '13 at 22:36
  • @hwrdprkns What if I need access to instance variables on YourFragment? How would you use getters and setters in this situation? – IgorGanapolsky Sep 11 '14 at 16:35
  • Although I agree with @hwrdprkns that fragments are reusable components, in most cases when using PagerAdapters and its implementations such as FragmentPagerAdapeter it is ok to use singleton for fragments because getItem will create new instance on configChange. Please correct me if I'm wrong, I don't see a lot of fragment implementations in case like that where developers use singletons but from few solutions that I saw on SO this doesn't reuse fragments as in "normal" fragment usage – DoubleK Dec 02 '15 at 10:24