Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not perform this action after onSaveInstanceState (onClick preference)

Here is my PreferenceActivity (Inner class of my Main Activity)

public static class TestSettings extends PreferenceActivity implements Preference.OnPreferenceClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref);
        getPreferenceManager().findPreference("key").setOnPreferenceClickListener(this);

    }

Here is my onPreferenceClick:

@Override
public boolean onPreferenceClick(Preference preference) {
    FragmentClass fc = new FragmentClass();
    fc.show(fm, "TAG");
    return false;
}

And here is my FragmentClass ( TimePicker dialog ):

public class FragmentClass extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new TimePickerDialog(getActivity(), this, 15, 00, false);
    }


    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Log.i("APP", "hour: "+hourOfDay+", min: "+minute);
    }
}

PROBLEM:

When I click on preference (defined in XML ), and when I handle onClick (I'm showing TimePickerDialog onClick) I got following exception:

  03-21 10:06:41.325: E/AndroidRuntime(11003): FATAL EXCEPTION: main
03-21 10:06:41.325: E/AndroidRuntime(11003): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.DialogFragment.show(DialogFragment.java:127)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.example.timepicker2.MainActivity$TestSettings.onPreferenceClick(MainActivity.java:110)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.preference.Preference.performClick(Preference.java:951)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Handler.handleCallback(Handler.java:725)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Looper.loop(Looper.java:137)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at java.lang.reflect.Method.invoke(Method.java:511)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at dalvik.system.NativeStart.main(Native Method)

Code error points to method show() in onPreferenceClick method.

Why does this happen and how can I fix this?

EDIT:

Prefs.xml file

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >


    <CheckBoxPreference android:title="Checkbox" android:summary="Checkbox"/>
    <EditTextPreference android:summary="time" android:dialogTitle="Hello"  />
    <Preference android:title="Prefff" android:key="key"/>
</PreferenceScreen>

What i've tried:

I've tried solution with most votes, which i found here getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState"

   @Override
protected void onSaveInstanceState(Bundle outState) {
    //No call for super(). Bug on API Level > 11.
}

But it's still not working. Not even on API < 11. I'm still getting already mentioned exception.

like image 277
rootpanthera Avatar asked Mar 31 '13 15:03

rootpanthera


1 Answers

Going out on a bit of a limb here:

// this is in your TestSettings class
public boolean onPreferenceClick(Preference preference) {
    FragmentClass fc = new FragmentClass();
    // getFragmentManager() here ensures you are using the FragmentManager
    // associated with TestSettings activity and not your MainActivity
    fc.show(getFragmentManager(), "TAG");
    return false;
}

The key being getFragmentManager()

Ensures that you are using the FragmentManager associated with the TestSettings Activity. I have a feeling that you are using the FragmentManager instance from MainActivity (seeing as TestSettings is an inner class of MainActivity this isn't that crazy of a guess... maybe).

like image 125
Brett Duncavage Avatar answered Sep 30 '22 14:09

Brett Duncavage