Code Monkey home page Code Monkey logo

Comments (10)

gregkorossy avatar gregkorossy commented on June 2, 2024 2

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()
    }
}

from android-support-preference-v7-fix.

gregkorossy avatar gregkorossy commented on June 2, 2024

Is it possible you are using it from a tag in a layout XML? The sample app doesn't crash.

from android-support-preference-v7-fix.

proninyaroslav avatar proninyaroslav commented on June 2, 2024

My example:

<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.preference.ListPreference
        android:title="Download Details"
        android:summary="Select the kind of data that you would like to download"
        android:key="downloadType"
        android:defaultValue="1"
        app:entries="@array/listArray"
        app:entryValues="@array/listValues" />
</android.support.v7.preference.PreferenceScreen>

from android-support-preference-v7-fix.

gregkorossy avatar gregkorossy commented on June 2, 2024

I see the problem: don't use fully qualified package name. It's not recommended in the original implementation either, and causes troubles in the fix because the PreferenceManager normally resolves the preferences using the fixed classes available in my fix package, then, if it can't find there, in the original packages. By specifying the package name in your XML you effectively eliminate this behavior which can result in crashes, what you had just experienced.
Also, you don't need the app namespace.

So your example would be fixed like this:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:title="Download Details"
        android:summary="Select the kind of data that you would like to download"
        android:key="downloadType"
        android:defaultValue="1"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues" />
</PreferenceScreen>

from android-support-preference-v7-fix.

proninyaroslav avatar proninyaroslav commented on June 2, 2024

Alas, it didn't help.

from android-support-preference-v7-fix.

gregkorossy avatar gregkorossy commented on June 2, 2024

Try the sample app here, it should work.

Also, please provide your device type and/or Android version. How do you instantiate and add your Fragment to the FragmentManager?

from android-support-preference-v7-fix.

proninyaroslav avatar proninyaroslav commented on June 2, 2024

Android 4.4 - 6.1 (all in range).
I added fragment the following way:

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

Fragment class:

public class StorageSettingsFragment extends PreferenceFragmentCompat
{
    public static StorageSettingsFragment newInstance()
    {
        StorageSettingsFragment fragment = new StorageSettingsFragment();

        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreatePreferencesFix(Bundle savedInstanceState, String rootKey)
    {
        addPreferencesFromResource(R.xml.pref_storage);
    }
}

from android-support-preference-v7-fix.

proninyaroslav avatar proninyaroslav commented on June 2, 2024

Yes, you're almost right. Now code works correctly. Since the Support Library doesn't support two-pane interface, I write your implementation for application. Preference opens in activity or replaced in the main window container.

P.S: I've got one more question about activatedBackgroundIndicator, so as not to open a new issue, I will write it here:
the only difficulty I encountered - highlighting the selected preference item (aka header in PreferenceActivity).
For example (Email app):
screenshot_20160819-134331
Is it possible to provide this opportunity in PreferenceFragmentCompat?

from android-support-preference-v7-fix.

gregkorossy avatar gregkorossy commented on June 2, 2024

Well, the PreferenceFragmentCompat is built on top of a RecyclerView which provides no method for setting the list selection. There are multiple problems to solve:

  1. The selected item position has to be stored and applied in onBindViewHolder(...) of PreferenceGroupAdapter using View.setSelected(...) on the item view
  2. The selectableItemBackground, which is used as a background drawable on preferences, has no selector for "state_selected" state, thus a new LayerList drawable is needed to accommodate both this and the desired drawable (preferably a color one in your case).
  3. This still cannot change the text color of the preferences. Applying only the background with, for example, the accent color makes the background color change when one sets selection but the preference's text color stays the same, which may look wrong. This can be resolved manually by iterating over the children of the preference's root view and setting the text color to an appropriate one, like using a ColorStateList.

I tested it on API 21, so it's possible to create such a thing, though I don't know whether it should be part of this lib or not.

from android-support-preference-v7-fix.

proninyaroslav avatar proninyaroslav commented on June 2, 2024

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

from android-support-preference-v7-fix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.