7

I have a fragment with EditText and add it into the layout using transactions. But if I rotate to landscape the soft keyboard disappears.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (getSupportFragmentManager().findFragmentById(R.id.fragmentContainer) == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragmentContainer, new FragmentWithEditText())
                    .commit();
        }
    }
}

I want keyboard state still unchanged after rotate using fragment transactions. Because if I don't use transactions, but add a fragment straight in the layout, the keyboard not disappeared.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:tag="fragmentWithKeyboard"
        android:name="com.test.FragmentWithEditText"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

I already tried to use android:windowSoftInputMode="stateUnchanged" or android:configChanges="keyboardHidden|orientation", but didn't help.

Also I wrote a sample app with this behavior https://github.com/anton9088/FragmentAndKeyboard

Similar questions:

Retain soft-input/IME state on Orientation change

Keyboard dismissed on rotation to landscape mode Android

Community
  • 1
  • 1
user1275972
  • 131
  • 1
  • 8
  • use this android:windowSoftInputMode="stateUnchanged|adjustResize" – Radhey Nov 08 '16 at 05:22
  • 1
    android:windowSoftInputMode="stateUnchanged|adjustResize" not working, but if I use stateAlwaysVisible keyboard appeared in portrait, but not in landscape – user1275972 Nov 08 '16 at 11:40
  • @Radhey Strangely this works only if parent Activity has EditText in its layout. If there is only EditText in fragment and it has focus and keyboard is shown, after orientation change, keyboard hides. – Bresiu May 30 '17 at 21:15
  • @user1275972 Are you have any solution? – Harco Apr 26 '19 at 07:41

3 Answers3

1

Try to set your EditText's attribute freezesText value to true.

You can also add focus in onViewCreated callback manually

Alexander Blinov
  • 393
  • 3
  • 13
  • The problem is not in focus, EditText has focus both before and after rotate – user1275972 Nov 08 '16 at 11:37
  • @user1275972 but it is losing focus during fragment pause. This may trigger keyboard to disappear as there is no Editable widget during fragments recreation. But strangely it is working if EditText is in Activity with configChanges in its manifest. – Bresiu May 30 '17 at 21:19
1

Problem

The problem is that our view should has windowSoftInputMode set to stateUnchanged which means:

The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.

In an activity it can be obtained by simply change in AndroidManifest.xml by adding this:

<activity
    ...
    android:windowSoftInputMode="stateUnchanged"/>

Unfortunately it will not work for fragments.

Solution for DialogFragment

If your fragment is a DialogFragment you can obtain it by adding this code to your onCreateDialog method while you have already created a dialog:

Window window = dialog.getWindow();
if (window != null) {
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED);
}
Wojtek
  • 1,210
  • 3
  • 18
  • 23
-1

Change below lines in your manifest i am sure it will helps you

<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize">
</activity>

So replace your

android:configChanges="keyboardHidden|orientation" To android:configChanges="orientation|screenSize"

Kishan Soni
  • 816
  • 1
  • 6
  • 19