Skip to content

Commit

Permalink
Merge pull request #84 from ernestoyaquello/feature/82-global-layout-…
Browse files Browse the repository at this point in the history
…memory-leak

Fixed memory leak caused by global layout listener not being removed
  • Loading branch information
ernestoyaquello authored Jul 5, 2019
2 parents f5699b6 + 5fa4834 commit 0945a8b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class VerticalStepperFormView extends LinearLayout {
FormStyle style;

private StepperFormListener listener;
private KeyboardTogglingObserver keyboardTogglingObserver;
private List<StepHelper> stepHelpers;
private boolean initialized;

private LinearLayout formContentView;
private ScrollView stepsScrollView;
Expand Down Expand Up @@ -455,6 +457,8 @@ private void onConstructed(Context context, AttributeSet attrs, int defStyleAttr
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.vertical_stepper_form_layout, this, true);

keyboardTogglingObserver = new KeyboardTogglingObserver();

style = new FormStyle();

// Set the default values for all the style properties
Expand Down Expand Up @@ -663,15 +667,14 @@ void initializeForm(StepperFormListener listener, StepHelper[] stepsArray) {
hideBottomNavigation();
}

keyboardIsOpen = isKeyboardOpen();
setObserverForKeyboard();

for (int i = 0; i < stepHelpers.size(); i++) {
View stepLayout = initializeStepHelper(i);
formContentView.addView(stepLayout);
}

goToStep(0, false);

initialized = true;
}

private View initializeStepHelper(int position) {
Expand Down Expand Up @@ -765,28 +768,6 @@ private void enableOrDisableLastStepNextButton() {
}
}

private void setObserverForKeyboard() {
getRootView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
boolean keyboardWasOpen = keyboardIsOpen;
keyboardIsOpen = isKeyboardOpen();
if (keyboardIsOpen != keyboardWasOpen) {
scrollToOpenStepIfNecessary(true);
}
}
});
}

private boolean isKeyboardOpen() {
Rect r = new Rect();
formContentView.getWindowVisibleDisplayFrame(r);
int screenHeight = formContentView.getRootView().getHeight();
int keyboardHeight = screenHeight - r.bottom;

return keyboardHeight > screenHeight * 0.2;
}

private synchronized void attemptToCompleteForm(boolean isCancellation) {
if (formCompleted) {
return;
Expand Down Expand Up @@ -857,6 +838,33 @@ public void onClick(View view) {
goToNextStep(true);
}
});

addObserverForKeyboard();
}

@Override
protected void onDetachedFromWindow() {
removeObserverForKeyboard();

super.onDetachedFromWindow();
}

private void addObserverForKeyboard() {
keyboardIsOpen = isKeyboardOpen();
getRootView().getViewTreeObserver().addOnGlobalLayoutListener(keyboardTogglingObserver);
}

private void removeObserverForKeyboard() {
getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(keyboardTogglingObserver);
}

private boolean isKeyboardOpen() {
Rect r = new Rect();
formContentView.getWindowVisibleDisplayFrame(r);
int screenHeight = formContentView.getRootView().getHeight();
int keyboardHeight = screenHeight - r.bottom;

return keyboardHeight > screenHeight * 0.2;
}

private void restoreFromState(
Expand Down Expand Up @@ -1025,4 +1033,16 @@ class FormStyle {
boolean allowStepOpeningOnHeaderClick;
float alphaOfDisabledElements;
}

private class KeyboardTogglingObserver implements ViewTreeObserver.OnGlobalLayoutListener {

@Override
public void onGlobalLayout() {
boolean keyboardWasOpen = keyboardIsOpen;
keyboardIsOpen = isKeyboardOpen();
if (initialized && keyboardIsOpen != keyboardWasOpen) {
scrollToOpenStepIfNecessary(true);
}
}
}
}

0 comments on commit 0945a8b

Please sign in to comment.