Illegalstateexception Fragment must be public static class to be properly recreated from instance state

Viewed 3295

below is the code which is throwing the error message

"java.lang.illegal state exception:Fragment must be public static class to be properly recreated from instance state "

The below code snippet throwing error only after i included **compile 'com.google.android.gms:play-services-ads:11.8.0'** in build.gradle file, else working fine.

public void selectDate(View view) {
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");
        }

@SuppressLint("ValidFragment")
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final Calendar calendar = Calendar.getInstance();
                int yy = calendar.get(Calendar.YEAR);
                int mm = calendar.get(Calendar.MONTH);
                int dd = calendar.get(Calendar.DAY_OF_MONTH);
                return new DatePickerDialog(getActivity(), this, yy, mm, dd);
            }
2 Answers

Change this:

@SuppressLint("ValidFragment")
public class SelectDateFragment ...

to this:

public static class SelectDateFragment ...

What's happening here is that the Android framework is trying to "recreate" your Fragment from saved instance state, and it can only do that if a number of things are true. One of the many things that needs to be true is that your Fragment doesn't "need" anything else in order to be instantiated, but all non-static inner classes "need" an enclosing instance of the outer class in order to be instantiated.

If adding the static keyword to your Fragment's class definition causes other problems, you'll have to address those separately. Perhaps your enclosing Activity class has a method you want to call; you can replace e.g.

someActivityCall();

with

MyActivity activity = (MyActivity) getActivity();
activity.someActivityCall();

Suppose that if you remove suppress annotation you will get that problem every time (not only when build.gradle changed). In your case, suppose, declaration of SelectDateFragment class is placed inside another class - it's named inner class - and have no static qualifier. So there is restriction for Fragment not to be inner non-static class. To fix problem you need to declare your fragment as public static class (if it's placed inside another class declaration).

Related