Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop fragment from being recreated after resume?

I am using several fragments to be dynamically added into activity. Everything works fine, when I press back-button, the fragments go to backstack. And when I resume it, it appears. But everytime on Resume, it is recreating the fragment and call onCreateView. I know it is a normal behavior of the fragment lifecycle.

This is my onCreateView:

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

I want to stop those fragments from recreating. I tried with onSavedInstanstate but nothing is working. How can I accomplish that?

like image 705
Kaidul Avatar asked Aug 25 '13 10:08

Kaidul


People also ask

How do you stop a fragment from recreating?

Calling setRetainInstance(true) will prevent the Fragment from being re-created.

How do you stop a fragment?

The Android Fragment States. Stop State: When an activity is stopped, its related fragments are stopped also. FragmentTransaction's remove() , replace() method can also make a fragment runs into the stop state. A stopped fragment is invisible, it can be recycled by the android OS.

What is difference between onCreate and onCreateView in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

Are fragments destroyed when activity is destroyed?

As Fragment is embedded inside an Activity, it will be killed when Activity is killed. As contents of activity are first killed, fragment will be destroyed just before activity gets destroyed.


2 Answers

In the Activity's onCreateView set the savedInstanceState to null before calling the super method. You could also remove only the keys "android:viewHierarchyState" and "android:fragments" from the savedInstanceState bundle. Here is code for the simple solution, nulling the state:

@Override
public void onCreate(Bundle savedInstanceState)
{
    savedInstanceState = null;
    super.onCreate(savedInstanceState);

    ...
}
like image 52
Arne Evertsson Avatar answered Oct 24 '22 06:10

Arne Evertsson


Iam using 5 fragments and working for me good as I was facing the same issue before..

public class MyFragmentView1 extends Fragment {

    View v;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, 
                @Nullable Bundle savedInstanceState) {
        if (v == null) 
            v = inflater.inflate(R.layout.my_fragment_view_layout, 
                container, false
            );
        return v;
    }
}

I put the view variable inside class and inflating it as new only if the view instance is null or otherwise use the one created before

like image 33
Abdul Saleem Avatar answered Oct 24 '22 08:10

Abdul Saleem