-1

In my base Activity class I have links to all fragments that I call from menu.

Something like this:

BaseFragment mProjects;
BaseFragment mProfile;

public BaseFragment getFragment(FragmentType type) {
        BaseFragment fr = null;
        switch (type) {
            case PROFILE:
                fr = new Profile();
                break;
            case PROJECTS:
                fr = new Projects();
                break;
            default:
                return null;
        }

        return fr;
    }

So, i create instance of fragments that accessible from menu one time. Is it good? Or i should create fragment every time? If so, then how can i save state of fragment (data, position)? Cause when i use replace from fragment manager, onSaveInstanceState of replaced fragment doesn't called. So when i will reopen it, there is no saved state.

Btw, I'm using onSaveInstanceState in Activity:

@Override
public void onSaveInstanceState(Bundle saveInstanceState) {

    // doing things
    super.onSaveInstanceState(saveInstanceState);
}

Anyway if i don't store links to fragments onSaveInstanceState of fragment will never called. So, i can store state of fragment in onPause method, but it seems bad solution.

UPD: it's not opinion based question. There should be best practice for using fragment. So i asked for this.

2 Answers2

0

Maybe you make a mistake in your FragmentActivity.

Perhaps you override onSaveInstanceState without call super.onSaveInstanceState(Bundle outState) in your FragmentActivity

This will cause the method ** onSaveInstanceState** not called in fragment

UPDATE Alternatively you could use hide/show instead of replace when you want to show new fragments... the hided fragment is not killed

Tommaso Resti
  • 5,800
  • 4
  • 18
  • 34
0

Before you replace a currently shown fragment, you need to getSupportFragmentManager().putFragment(Bundle, String, Fragment), this will store that fragment into a Bundle which you can keep a reference to, and save to your activity's instance state.

In your getFragment method, first check to see if the bundle you have been saving your fragments into contains the fragment you want to create - getSupportFragmentManager().getFragment(Bundle, String). This will give you a fragment you can return and replace. If not, create a new instance of the fragment as you currently are.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • But why reference to Bundle better then direct reference to fragment? Or why is it better then HashMap of fragments? – Suvitruf - Andrei Apanasik Feb 10 '15 at 11:58
  • The FragmentManager holds an array of your active fragments, and will handle their states. The put and get methods just store an additional int (the id of the fragment). Keeping your own references to the fragments is probably easy to get out of sync, and potentially doubles the amount of saved instance state if you try to save them again. – FunkTheMonk Feb 10 '15 at 13:25
  • I tried it. But for all keys that i added using putFragment it generates same int value. And that's why it returns wrong value in getFragment... – Suvitruf - Andrei Apanasik Feb 10 '15 at 14:09