Pressing back button on Android to close/hide the keyboard is clearing my input field

I have two methods that fire the Unity Event for that input field, try to save the input field value into a variable and then give that value back to Input Field when back button is pressed, but it doesn’t work on Android. It works just fine with Unity Editor

public string passwordHolder = “”;

public void OnEditting()

   {
     if (Application.platform == RuntimePlatform.Android) 
      {
        if (!Input.GetKeyDown(KeyCode.Escape))
         {
            passwordHolder = passwordText.text;
         }
     }
 }

public void OnEndEdit()

{

    if (Application.platform == RuntimePlatform.Android) 
          {
               if (Input.GetKeyDown(KeyCode.Escape))
              {              
                     passwordText.text = passwordHolder;               
              }
         }
}

What am I doing wrong?

The “Input” does not work as normal when the keyboard is open. So you can not do the check you do with the “ESC” / back button.

What I have done is creating a method to listen to an event of the keyboard status changing. If it is going to hide (Canceled), I keep the previous text in the field.

I add the script to the object with the InputField component and this is the code that I have put in “Start”:

        placeHolder = (TextMeshProUGUI) inputField.placeholder;
        inputField.onEndEdit.AddListener(EndEdit);
        inputField.onValueChanged.AddListener(Editing);
        inputField.onTouchScreenKeyboardStatusChanged.AddListener(ReportChangeStatus);

And this is the code of the methods:

    private void ReportChangeStatus(TouchScreenKeyboard.Status newStatus)
    {
        if (newStatus == TouchScreenKeyboard.Status.Canceled)
            keepOldTextInField = true;
    }

    private void Editing(string currentText)
    {
        oldEditText = editText;
        editText = currentText;
    }
  
    private void EndEdit(string currentText)
    {
        if (keepOldTextInField && !string.IsNullOrEmpty(oldEditText))
        {
            //IMPORTANT ORDER
            editText = oldEditText;
            inputField.text = oldEditText;
            
            keepOldTextInField = false;
        }
    }

It works for me and I hope it does for you aswell.

I came looking for a similar problem.
Don’t know if this is what you where looking for but it works for me.

public InputField inputfield;

//Call method  in editor
public void MethodCalled_OnEndEdit()
{
   if (inputfield.wasCanceled)
   {
    //Do stuff
     return;
   }
}