10

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.

Community
  • 1
  • 1
rootpanthera
  • 2,731
  • 10
  • 33
  • 65
  • show this file pref.xml – Dhaval Parmar Apr 01 '13 at 06:07
  • I edited my question. I still didn't find a solution to this. – rootpanthera Apr 01 '13 at 13:49
  • Try this code http://www.mediafire.com/?muvaeodxz2mj36k – Manoj Kumar Apr 01 '13 at 13:58
  • @Mojo Your code seems to work fine, but i'm afraid that this is not what i'm looking for. Your preference.xml file has EditTextPreferences(which has built in dialog), and its dialog always works fine. I'm creating my own preference (TimePicker dialog) and it does not work. – rootpanthera Apr 01 '13 at 14:08
  • @rootpanthera Then you use Shared Preferences which is a good option in your case – Manoj Kumar Apr 02 '13 at 05:58
  • @rootpanthera I'm just throwing a guess here, what if you return a true from onPreferenceClick? EDIT: also have a look at the excepted anser here: http://stackoverflow.com/questions/14177781/java-lang-illegalstateexception-can-not-perform-this-action-after-onsaveinstanc again, not sure it fit's your case, but if it does... –  Apr 02 '13 at 23:13
  • Also look at the comments there and the answer by the original poster in response to the accepted answer. –  Apr 02 '13 at 23:19

1 Answers1

1

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).

Brett Duncavage
  • 2,163
  • 14
  • 16
  • You are right. I'm using getFragmentManager() from my MainActivity which is TOP class. I have nested class TestSettings (extends preference activity ). Is this the wrong approach, or..? Because i still didn't find a solution to this. – rootpanthera Apr 03 '13 at 08:13
  • Do not use the FragmentManger from your MainActivity in your TestSettings activity. FragmentManager instances are attached to their activities and their corresponding life cycles. – Brett Duncavage Apr 03 '13 at 08:32