Skip to content

Commit e366099

Browse files
committed
Fix fragment mode if onAttach is called before onCreateView
1 parent aced045 commit e366099

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

CHANGELOG.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Please see the github repository for a complete list of changes: https://github.com/simonpoole/OpeningHoursFragment/commits/master
22

3+
0.13.2:
4+
- Fix fragment mode if onAttach is called before onCreateView
5+
36
0.13.0:
47
- Support running as a 1 level deep nested Fragment
58
- Update translations

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ repositories {
5050

5151
``` groovy
5252
dependencies {
53-
compile "ch.poole:OpeningHoursFragment:0.12.0"
53+
compile "ch.poole:OpeningHoursFragment:0.13.2"
5454
}
5555
```
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

lib/build.gradle

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply plugin: 'maven-publish'
99
apply plugin: 'signing'
1010
apply plugin: "jacoco"
1111

12-
version = '0.13.0'
12+
version = '0.13.2'
1313
def libName = "OpeningHoursFragment"
1414

1515
task updateTranslations(type: Exec) {
@@ -29,8 +29,8 @@ android {
2929
compileSdkVersion 28
3030

3131
defaultConfig {
32-
minSdkVersion 14
33-
targetSdkVersion 28
32+
minSdkVersion 16
33+
targetSdkVersion 31
3434
versionCode 100
3535
versionName "${project.version}"
3636
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -100,7 +100,6 @@ dependencies {
100100
implementation "ch.poole:OpeningHoursParser:0.26.0"
101101
implementation "ch.poole.android:rangebar:0.1.6"
102102
implementation 'cn.carbswang.android:NumberPickerView:1.2.0'
103-
implementation "com.google.code.gson:gson:2.8.5"
104103
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
105104

106105
// Instrumentation tests

lib/src/main/java/ch/poole/openinghoursfragment/OpeningHoursFragment.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,6 @@ public static OpeningHoursFragment newInstanceForFragment(@NonNull ValueWithDesc
378378
return f;
379379
}
380380

381-
@Override
382-
public void onAttach(Context context) {
383-
super.onAttach(context);
384-
Log.d(DEBUG_TAG, "onAttach");
385-
if (!useFragmentCallback) {
386-
try {
387-
saveListener = (OnSaveListener) context;
388-
} catch (ClassCastException e) {
389-
throw new ClassCastException(context.toString() + " must implement OnSaveListener");
390-
}
391-
}
392-
}
393-
394381
@Override
395382
public void onCreate(Bundle savedInstanceState) {
396383
super.onCreate(savedInstanceState);
@@ -412,6 +399,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
412399
@SuppressLint("InflateParams")
413400
@Override
414401
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
402+
Log.d(DEBUG_TAG, "onCreateView");
415403
int initialRule = -1;
416404
if (savedInstanceState != null) {
417405
Log.d(DEBUG_TAG, "Restoring from saved state");
@@ -506,13 +494,20 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
506494
AppCompatButton cancel = (AppCompatButton) openingHoursLayout.findViewById(R.id.cancel);
507495
cancel.setOnClickListener(v -> dismiss());
508496

509-
if (useFragmentCallback) {
510-
Fragment fragment = getParentFragment();
511-
// we may be nested one or two levels deep
512-
if (!(fragment instanceof OnSaveListener)) {
513-
fragment = fragment.getParentFragment();
497+
Object listener = null;
498+
try {
499+
if (useFragmentCallback) {
500+
listener = getParentFragment();
501+
// we may be nested one or two levels deep
502+
if (!(listener instanceof OnSaveListener)) {
503+
listener = ((Fragment) listener).getParentFragment();
504+
}
505+
} else {
506+
listener = getContext();
514507
}
515-
saveListener = (OnSaveListener) fragment;
508+
saveListener = (OnSaveListener) listener;
509+
} catch (ClassCastException e) {
510+
throw new ClassCastException(listener != null ? listener.getClass().getCanonicalName() + " must implement OnSaveListener" : "OnSaveListener is null");
516511
}
517512

518513
saveButton = (AppCompatButton) openingHoursLayout.findViewById(R.id.save);

0 commit comments

Comments
 (0)