Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard disappears on orientation change

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

like image 480
user1275972 Avatar asked Nov 08 '16 00:11

user1275972


People also ask

Why an Activity is destroyed and recreated when you rotate your screen?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.

What happens when screen orientation changes in Android?

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).

What happened to the app's Activity when the user rotate the device?

Some device configurations change while the application is running, such as when the device changes its orientation. Such a change restarts the activity by calling all its methods again.


2 Answers

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

You can also add focus in onViewCreated callback manually

like image 94
Alexander Blinov Avatar answered Oct 25 '22 19:10

Alexander Blinov


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);
}
like image 20
Wojtek Avatar answered Oct 25 '22 19:10

Wojtek