Skip to content

Text field is empty after selecting the same country twice #28

@pchmielowski

Description

@pchmielowski

Code

Layout:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

Activity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val kit = PhoneNumberKit.Builder(this).build()
        kit.attachToInput(binding.inputLayout, "us")
        kit.setupCountryPicker(this)
    }
}

Reproduction steps

  1. Run the app.
  2. Click on the flag
  3. Choose any country
  4. Click on the flag again
  5. Choose the same country as in step 3.

Result

The text field is empty

Expected behavior

The text field contains area code of the selected country.

Possible cause:

After country is selected, the text field is cleared:

Then, the value of state is updated with the correct country code:


The problem is the following: the new value of state is exactly the same as the previous one (as we are reselecting the same country). MutableStateFlow does not emit a new value to its collectors if we set the same value.
That's why the following line is not called:
inputValue = state.country.code.toString()

And the text field stays empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions