JPM
JPM

Reputation: 1482

AutoCompleteTextView - Crash When Rotating Device While Popup Window is Visible

I am experiencing a crash whenever I rotate a device while the AutoCompleteTextView dropdown is displayed.

I can recreate this every time by:

  1. Tap the AutoCompleteTextView to bring up a dropdown of suggestions
  2. Do not make a selection
  3. Rotate the device

Stacktrace:

     Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/criteria_text_input_layout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Name"
    app:errorEnabled="true">

    <AutoCompleteTextView
        android:id="@+id/criteria_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="LabelFor" />

</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

I have searched for that exception and it appears this can happen when you use getApplicationContext(), but all my adapters use getContext(). I have also tried dismissing the dropdown on configuration changes, but to no avail. Any ideas?

Upvotes: 0

Views: 545

Answers (2)

JPM
JPM

Reputation: 1482

Turns out this is a problem with Samsung devices (which is what I was using to test). I could not find out exactly where the problem was in the code, but I did notice an error in the logs:

sendUserActionEvent() mView == null

So I did another search and found that it's a known bug on some Android devices where mView can be null so one of the suggested solutions was to add the following to the manifest:

android:configChanges="orientation|screenSize"

This seems to have fixed my issue.

EDIT: Using this introduces another problem as rotating the device no longer allows me to restore state properly. So I am now back at square one.

EDIT #2: Seems this problem happens during restoring of state of the TextInputLayout and AutoCompleteTextView. Another solution is to disable saving of state by adding the following to the view(s):

    android:saveEnabled="false"

By adding this and removing the configChanges above allows me to restore state as I was expecting.

Upvotes: 1

Bali
Bali

Reputation: 759

BadTokenException happens when your popup or dialog is showing and activity is destroyed. That's why we usually close popups and dialogs in onDestroy(). e.g.

if(dialog.isShowing()) {
   dialog.dismiss();
}

So Add below line in your onDestroy() for AutoCompleteTextView.

yourAutoCompleteTextView.dismissDropDown();

Upvotes: 1

Related Questions