question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash in ListPreference during rotation

See original GitHub issue

Hello. If you rotate device 2 times with the open dialog then occurs crash:

java.lang.IllegalStateException: Failure saving state: ListPreferenceDialogFragmentCompat{9d3f3e08 #1 android.support.v7.preference.PreferenceFragment.DIALOG} has target not in fragment manager: SettingsFragment{9d3f3c08}
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1935)
at android.support.v4.app.FragmentController.saveAllState(FragmentController.java:134)
at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:568)
at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:511)
at android.app.Activity.performSaveInstanceState(Activity.java:1153)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1223)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3712)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Gericopcommented, Aug 20, 2016

I see the problem and is not related to this library nor any other libs. The problem is the way you create / use your StorageSettingsFragment. You probably use the fragment adder in a similar manner in your Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // [...]

    StorageSettingsFragment fragment = StorageSettingsFragment.newInstance();

    getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment).commit()
}

The problem with this is that when you rotate your phone, a new fragment gets created every time which replaces the old one. This won’t crash until you have a child fragment of that fragment (in this case it’s ListPreferenceDialogFragmentCompat) which has the old fragment as a target set.

To solve the problem, you just have to check whether it’s the first time onCreate(...) is called. It’s pretty simple with the received argument of savedInstanceState. If it’s null, it’s the first time. Nice, huh? In your case, the actual fix looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // [...]

    if (savedInstanceState == null) {
        StorageSettingsFragment fragment = StorageSettingsFragment.newInstance();

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment).commit()
    }
}
0reactions
proninyaroslavcommented, Aug 21, 2016

Thanks for the tip. I’ll try to do either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Crash on rotation when PreferenceScreen has key
I have preference screen with key so I can show/hide it based on debug state. Kotlin. caller.findPreference("debug_key_screen").isVisible = ...
Read more >
Prevent crash on screen rotation when showing panic app selector ...
Fixes this crash: Caused by: java.lang.IllegalStateException: ListPreference requires an entries array and an entryValues array. at android.preference.
Read more >
Androidx.Preference.Listpreference Dialog Crashes - ADocLib
When I click on Preferred Audioplayer then rotate the screen to landscape view the app crashes and exit.How to reproduce.Download the app from...
Read more >
Preference components and attributes - Android Developers
This topic describes some of the most commonly-used Preference components and attributes used when building a settings screen.
Read more >
[MOD]270/360 Rotation mod 2.1 (Now fixed and 100% working ...
Builds fine just crashes as rotation settings are incomplete in the 3rd smali ... AccelerometerMode uses HtcListPreference not normal android ListPreference ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found