0

When an entry is focused and you hit back, the only thing that happens is unfocus that entry. Hitting again the back button and then it is actually triggered.

Just onfocussing the entry by tapping another spot on the screen and than hitting back does trigger the back button.

We would like the back-button to be triggered even when an entry is focused. Any suggestions how to solve this ?

BrilBroeder
  • 1,409
  • 3
  • 18
  • 37
  • 1
    That is just how android behaves, and I guess its better if you keep it this way, in any case, if you want to change this behaviour you might need some Android level code in your activity – FreakyAli Jun 18 '19 at 09:35
  • yes,this is default Android behavior,if you still want to implement this requirement, you can customize the edittext with the answers below. – Leo Zhu Jun 19 '19 at 01:03

3 Answers3

1

This is how Android apps work, not Xamarin.Forms.

For more control you would need to override OnKeyPreIme in your Activity in Android project, detect that this situation has happened and handle it. Here is how it is done in Java: EditText with soft keyboard and "Back" button

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
1

you could use CustomRender,and Custom EditText,overwrite the OnKeyPreIme method

1.in .Android custom a EditText,here named MyEditText:

class MyEditext :EditText
{
    protected MyEditext(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public MyEditext(Context context) : base(context)
    {
    }

    public MyEditext(Context context, IAttributeSet attrs) : base(context, attrs)
    {
    }

    public MyEditext(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
    }

    public MyEditext(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
    {
    }

    //This method can be used to intercept keystroke events, typically handling the back key, updating the UI, and if you don't override this method, the IME will default to handling the keyboard (usually the keyboard disappears)
    public override bool OnKeyPreIme(Keycode keyCode, KeyEvent e)
    {
        if (keyCode == Keycode.Back)
        {
            if (listener !=null)
            {
                listener.onKeyBack();
                return true;
            }
        }
        return base.OnKeyPreIme(keyCode, e);
    }

    private OnEditTextKeyBackListener listener;
    public void SetOnEditTextKeyBackListener(OnEditTextKeyBackListener listener)
    {
        this.listener = listener;
    }

    public interface OnEditTextKeyBackListener
    {
        void onKeyBack();

    }
}

2.use CustomRender, create MyEntryRenderer :

    [assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRender))]
    namespace App18.Droid
    {
      class MyEntryRender : ViewRenderer<Entry,EditText>, MyEditext.OnEditTextKeyBackListener
        {
          private Context _context;
          public MyEntryRender(Context context) : base(context)
            {
             _context = context;
            }

          protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
              base.OnElementChanged(e);
              MyEditext editext = new MyEditext(_context);
              SetNativeControl(editext);
              editext.SetOnEditTextKeyBackListener(this);
            }


         public void onKeyBack()
            {
            _context.GetActivity().OnBackPressed();
            }
       } 
    }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
0

My suggestion would be to have CustomPage and CustomEntry as your components. Then in CustomPage you can override OnBackButtonPressed and in CustomEntry you can attach Focus/Unfocus event with your custom logic. This way is OK because then you can use it everywhere, just always check navigation stack.

{
        public CustomPage()
        {
            InitializeComponent();
        }

        protected override bool OnBackButtonPressed()
        {
            //your logic for navigating back
           //don't forget to do all the checks
        }
}

And then just use this page instead of regular ContentPage.

zpouip
  • 787
  • 5
  • 11
  • In the custom entry you can trap the unfocus, but you do not know why it was unfocused. It could be just the user selecting another field or hitting back. I found no way to trap the fact that hitting the back button was the trigger for the unfocus. Anyhow. Given the answers of others, stating this is default Android behavior, I guess the best solution is not to change it. Now convince the customer of this :-) – BrilBroeder Jun 18 '19 at 11:13
  • The thing is in Focus/Unfocus methods can be just used to set some global boolean variable like IsAnyEntryFocused, there is no need to some complex logic there. And then in OnBackButtonPressed, he can check for this global variable, so if IsAnyEntryFocused, do this, otherwise do that. – zpouip Jun 18 '19 at 11:24