Comments (10)
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.
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.
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.
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.
Alas, it didn't help.
from android-support-preference-v7-fix.
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.
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.
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):
Is it possible to provide this opportunity in PreferenceFragmentCompat?
from android-support-preference-v7-fix.
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:
- The selected item position has to be stored and applied in
onBindViewHolder(...)
ofPreferenceGroupAdapter
usingView.setSelected(...)
on the item view - The
selectableItemBackground
, which is used as a background drawable on preferences, has no selector for "state_selected" state, thus a newLayerList
drawable is needed to accommodate both this and the desired drawable (preferably a color one in your case). - 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.
Thanks for the tip. I'll try to do either.
from android-support-preference-v7-fix.
Related Issues (20)
- .setcancelable for Dialog? HOT 1
- how to show divider between preference when merged to androidX-1.0.0/support-28 HOT 1
- ColorPickerPreference dialog style HOT 1
- how to get items from custom view HOT 1
- [AndroidX] android:dependency doesn't change color of preference HOT 5
- PreferenceScreen deprecated in Android Q HOT 2
- Request to update the version of the preference library HOT 5
- Broken when using with latest AndroidX material alpha05 and appcompat alpha04 HOT 8
- keyboard do not hide when cancel button is clicked of editTextPreference HOT 2
- Issue with themes HOT 3
- NullPointerException in PreferenceFragmentCompat.traverseAndRefreshPrefs()
- Popup menu android doesn't work correctly HOT 1
- NumberFormatException in RingtonePreferenceDialogFragmentCompat HOT 7
- AndroidX 1.1.0 HOT 4
- Expand/Contract PreferenceCategory HOT 3
- Question about migrating from library fix:preference-v7:28.0.0.0 to preferencex:preferencex:1.1.0
- How do you get the value from ColorPickerPreference
- Exception: java.lang.NumberFormatException HOT 1
- How to add a ad in a Preference Screen? HOT 1
- ListPreference Crash on theme change (Light to Dark or Vice versa) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-support-preference-v7-fix.