Skip to content

Commit

Permalink
Fix fragment mode if onAttach is called before onCreateView
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Jul 17, 2022
1 parent aced045 commit e366099
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Please see the github repository for a complete list of changes: https://github.com/simonpoole/OpeningHoursFragment/commits/master

0.13.2:
- Fix fragment mode if onAttach is called before onCreateView

0.13.0:
- Support running as a 1 level deep nested Fragment
- Update translations
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ repositories {

``` groovy
dependencies {
compile "ch.poole:OpeningHoursFragment:0.12.0"
compile "ch.poole:OpeningHoursFragment:0.13.2"
}
```
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\:https://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\:https://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
7 changes: 3 additions & 4 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: "jacoco"

version = '0.13.0'
version = '0.13.2'
def libName = "OpeningHoursFragment"

task updateTranslations(type: Exec) {
Expand All @@ -29,8 +29,8 @@ android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 14
targetSdkVersion 28
minSdkVersion 16
targetSdkVersion 31
versionCode 100
versionName "${project.version}"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -100,7 +100,6 @@ dependencies {
implementation "ch.poole:OpeningHoursParser:0.26.0"
implementation "ch.poole.android:rangebar:0.1.6"
implementation 'cn.carbswang.android:NumberPickerView:1.2.0'
implementation "com.google.code.gson:gson:2.8.5"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Instrumentation tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,6 @@ public static OpeningHoursFragment newInstanceForFragment(@NonNull ValueWithDesc
return f;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(DEBUG_TAG, "onAttach");
if (!useFragmentCallback) {
try {
saveListener = (OnSaveListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnSaveListener");
}
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -412,6 +399,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(DEBUG_TAG, "onCreateView");
int initialRule = -1;
if (savedInstanceState != null) {
Log.d(DEBUG_TAG, "Restoring from saved state");
Expand Down Expand Up @@ -506,13 +494,20 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
AppCompatButton cancel = (AppCompatButton) openingHoursLayout.findViewById(R.id.cancel);
cancel.setOnClickListener(v -> dismiss());

if (useFragmentCallback) {
Fragment fragment = getParentFragment();
// we may be nested one or two levels deep
if (!(fragment instanceof OnSaveListener)) {
fragment = fragment.getParentFragment();
Object listener = null;
try {
if (useFragmentCallback) {
listener = getParentFragment();
// we may be nested one or two levels deep
if (!(listener instanceof OnSaveListener)) {
listener = ((Fragment) listener).getParentFragment();
}
} else {
listener = getContext();
}
saveListener = (OnSaveListener) fragment;
saveListener = (OnSaveListener) listener;
} catch (ClassCastException e) {
throw new ClassCastException(listener != null ? listener.getClass().getCanonicalName() + " must implement OnSaveListener" : "OnSaveListener is null");
}

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

0 comments on commit e366099

Please sign in to comment.