Fix issue with soft keyboard not showing from time to time on Android (#152)

This commit is contained in:
Krzysztof Magiera
2019-09-09 15:14:24 +02:00
committed by GitHub
parent de9bfc3021
commit 6508386424
2 changed files with 40 additions and 7 deletions

View File

@@ -1,16 +1,14 @@
package com.swmansion.rnscreens;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
@@ -63,6 +61,22 @@ public class Screen extends ViewGroup implements ReactPointerEventsView {
}
}
private static OnAttachStateChangeListener sShowSoftKeyboardOnAttach = new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
InputMethodManager inputMethodManager =
(InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, 0);
view.removeOnAttachStateChangeListener(sShowSoftKeyboardOnAttach);
}
@Override
public void onViewDetachedFromWindow(View view) {
}
};
private final Fragment mFragment;
private final EventDispatcher mEventDispatcher;
private @Nullable ScreenContainer mContainer;
@@ -88,6 +102,27 @@ public class Screen extends ViewGroup implements ReactPointerEventsView {
clearDisappearingChildren();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
// This method implements a workaround for RN's autoFocus functionality. Because of the way
// autoFocus is implemented it sometimes gets triggered before native text view is mounted. As
// a result Android ignores calls for opening soft keyboard and here we trigger it manually
// again after the screen is attached.
View view = getFocusedChild();
if (view != null) {
while (view instanceof ViewGroup) {
view = ((ViewGroup) view).getFocusedChild();
}
if (view instanceof TextView) {
TextView textView = (TextView) view;
if (textView.getShowSoftInputOnFocus()) {
textView.addOnAttachStateChangeListener(sShowSoftKeyboardOnAttach);
}
}
}
}
/**
* While transitioning this property allows to optimize rendering behavior on Android and provide
* a correct blending options for the animated screen. It is turned on automatically by the container

View File

@@ -8,8 +8,6 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import static com.swmansion.rnscreens.Screen.StackAnimation.*;
public class ScreenStack extends ScreenContainer {
private final ArrayList<Screen> mStack = new ArrayList<>();