Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText value is getting replaced with another edit text on backpress

I am in a situation here, I've two edit text in a fragment, on back press the value of first edit text is getting replaced by second edit text. Now both the edit text are having same values.

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:hint="hint" />

Above is my EditText, this EditText is inside a layout and I've included the layout in my fragment two times to get the EditText, on click of submit I replace this fragment with another one but on backpress when I come back to this fragment, the value of first edit box is getting replaced by second one. Now both has same values.

This is how I am replacing fragment.

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.layout_fragment, fragment, tag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
like image 792
DisplayName Avatar asked Oct 20 '25 02:10

DisplayName


2 Answers

Both of your EditTexts are using the same id. In your XML files you need to make sure that the id for both edit texts are different. Even if there are in separate XML files, they must have different ids.

For instance, you might change their declarations to...

<EditText
android:id="@+id/edit_text1"

<EditText
android:id="@+id/edit_text2"
like image 138
bremen_matt Avatar answered Oct 22 '25 03:10

bremen_matt


You should use different id for the EditText.

Assume the EditText is defined in edit_text_layout.xml, when including the layout, specify different id.

In fragment_layout.xml:

<include
    layout="@layout/edit_text_layout"
    android:id="@+id/edit_text_1" />

<include
    layout="@layout/edit_text_layout"
    android:id="@+id/edit_text_2" />

Explain: Android save a view's state in a SparseArray, the key is the view's id. When restoring the state, views have the same id will be get the same state value. If the view doesn't have an id (NO_ID), its state won't be saved or restored.

android.view.View.java

protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
    if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
        ......
        if (state != null) {
            ......
            container.put(mID, state);
        }
    }
}

protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
    if (mID != NO_ID) {
        Parcelable state = container.get(mID);
        if (state != null) {
            ......
            onRestoreInstanceState(state);
            ......
        }
    }
}
like image 29
wrkwrk Avatar answered Oct 22 '25 04:10

wrkwrk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!