'EditText Settext not working with Fragment

I have fragments for 3 states of a screen; Add, Edit and View. In Add, I create an entity and save it. Next time I open it in View mode and set the entity name using

EditText entityName = (EditText) view.findViewById(R.id.entityName);    
entityName.setText(entity.getEntityname());

I click on the edit button from View mode to open the Edit mode. I change the entity name here and save it. This brings me back to the view screen. But I find the entity name is not updated. I debug and found that entity.getEntityname() is having correct value. I am not sure why the edit text does not take new value.

Any ideas?

Note: I am using android version 2.2



Solution 1:[1]

There are some classes of View in Android should save their status when their container is detached. Fragment.onViewCreated() should be called before View.onSaveInstanceState(). So if you set a value in the method Fragment.onViewCreated(). The value should be cleared in the method View.onRestoreInstanceState(Parcelable state).

For example,class TextView,RecyclerView and so on.You can read the code of TextView.java:

    public Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();

    // Save state if we are forced to
    final boolean freezesText = getFreezesText();
    boolean hasSelection = false;
    int start = -1;
    int end = -1;
    ....
    if (freezesText || hasSelection) {
        SavedState ss = new SavedState(superState);
        ....
    }
    ....
   }

There are to params to control whether to save the state: "freezesText" and "hasSelection". TextView can't be selected,so hasSelection is false. the function ,getFreezesText(),return false in class TextView too. So,TextView would not save the state. the code of EditText.java:

    @Override
    public boolean getFreezesText() {
    return true;
    }

EditText return true,so EditText should save the state.

There some method to fix this bug:

1.implement EditText.getFreezesText() and return false,and clear the state of select in EditText

2.implement onSaveInstanceState of EditText, return null.like this:

 public Parcelable onSaveInstanceState() {
      super.onSaveInstanceState();
     return null;
 }

3.use EditText.setSaveEnable(false);

4.add param in xml " saveEnable='false'"

Solution 2:[2]

As mentioned earlier, the EditText appears to have an issue with resetting text in onCreateView.

This is because once a fragment is created , till the time we remove it from the backstack, its method onResume would be called as the view is not created again.

So the solution here is to reset the text in onResume. This will work on all times even if u lock and unlock ur screen while that fragment is open or you are coming back from another fragment

However if you are setting this data from a bundle it is better tonsave that value in an instance variable cause the bundle might come null amd u can gett null pointer issues then

Solution 3:[3]

According to the @TusharVengrulekar , this is how you must implement your Fragment

public class ActionBar extends Fragment {

private TextView lbl_title;
private String title;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_action_bar, container, false);
    title = "Contacts";
    lbl_title = (TextView) view.findViewById(R.id.lbl_title);

    return view;
}

@Override
public void onStart(){
    super.onStart();

    lbl_title.setText(title);
}

@Override
public void onResume(){
    super.onResume();
}
}<!---->

Solution 4:[4]

Also there's an issue in onActivityCreated. I reset edittext's content in onStart and it works.

Solution 5:[5]

onResume() or onStart() is fine for resetting the text on the EditText on popBackStack() but the issue is when the app goes into the background either of them will be triggered that would not be the expected behavior from the application. We can do something like this too, to reset the text on EditText-

override fun onViewStateRestored(savedInstanceState: Bundle?) {
   super.onViewStateRestored(savedInstanceState)
   binding.coolEt.setText("xyz")
}

Solution 6:[6]

This will work on fragment 100%

  override fun onResume() {
    super.onResume()
    Handler(Looper.getMainLooper()).postDelayed({
       editText.setText("Abc")
    }, 500)

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Himmat Gill
Solution 3 Paramjit Singh Rana
Solution 4
Solution 5 Etienne Kaiser
Solution 6 Sumedh Ulhe