Skip to content

Commit 38d195f

Browse files
authored
Merge pull request #186 from skydoves/fix/crashes
Prevent crashes when fragments are rapidly switched or garbage collected
2 parents d123f8f + 5ff07b7 commit 38d195f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

colorpickerview/src/main/java/com/skydoves/colorpickerview/preference/ColorPickerPreferenceManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public ColorPickerPreferenceManager clearSavedColor(String name) {
9797
* @return {@link ColorPickerPreferenceManager}.
9898
*/
9999
public ColorPickerPreferenceManager setSelectorPosition(String name, Point position) {
100+
if (position == null) {
101+
return colorPickerPreferenceManager;
102+
}
100103
sharedPreferences.edit().putInt(getSelectorXName(name), position.x).apply();
101104
sharedPreferences.edit().putInt(getSelectorYName(name), position.y).apply();
102105
return colorPickerPreferenceManager;
@@ -110,9 +113,11 @@ public ColorPickerPreferenceManager setSelectorPosition(String name, Point posit
110113
* @return the saved selector position.
111114
*/
112115
public Point getSelectorPosition(String name, Point defaultPoint) {
116+
int defaultX = defaultPoint != null ? defaultPoint.x : 0;
117+
int defaultY = defaultPoint != null ? defaultPoint.y : 0;
113118
return new Point(
114-
sharedPreferences.getInt(getSelectorXName(name), defaultPoint.x),
115-
sharedPreferences.getInt(getSelectorYName(name), defaultPoint.y));
119+
sharedPreferences.getInt(getSelectorXName(name), defaultX),
120+
sharedPreferences.getInt(getSelectorYName(name), defaultY));
116121
}
117122

118123
/**

0 commit comments

Comments
 (0)