Bug 22132 - Adding TextChanged and KeyPressed Event Handlers to EditText makes OnBackPressed not hitting
Summary: Adding TextChanged and KeyPressed Event Handlers to EditText makes OnBackPres...
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.16.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2014-08-15 05:11 UTC by Uday Gupta
Modified: 2014-08-16 03:40 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Uday Gupta 2014-08-15 05:11:21 UTC

I have been working on an app where there are 3 activities (let's say A, B, C)

1. A calls B using StartActivity(typeof(B))
2. B calls C using StartActivity(typeof(C)) and finish itself using this.finish()
3. C has a full screen EditText inside a LinearLayout. C has also overridden OnBackPressed where it calls this.finish() to complete itself and navigates back to A [as A is still in the BackStack]. Inside OnCreate Method of C, EventHandlers of EditText have been added TextChanged event and KeyPressed Event like below:

public void OnCreate(Bundle SavedInstanceBundle) 
    EditText editText = FindViewById<EditText>(Resource.Id.editText);
    editText.TextChanged += editText_TextChanged;
    editText.KeyPressed += editText_KeyPressed;

private void editText_TextChanged (object sender, TextChangedEventArgs e)
private void editText_KeyPressed (object sender, View.KeyEventArgs e)

public override void OnBackPressed() 

The problem here is that when the eventhandler code exist, the OnBackPressed is never executed when Hardware back is pressed. But, when the adding event handler code is removed for both the event, eventually OnBackPressed starts working properly.

This issue has really frustated me as i am not able to debug the issue at my end. I see this as a bug in Xamarin.Android framework.
Comment 1 Uday Gupta 2014-08-15 05:16:05 UTC
I have been trying to debug this issue for almost more than a week and its a blocker for my app development. Until this is fixed, i cannot progress anymore. Kindly check it at higher priority
Comment 2 Jonathan Pryor 2014-08-15 10:56:33 UTC
My apologies; you've hit an undocumented area. Please see the updated docs: http://developer.xamarin.com/guides/android/advanced_topics/api_design/#Events_and_Listeners

TL;DR: within editText_KeyPressed(), add the following line:


This will cause the back button to behave as you expect.

Details: When Xamarin.Android binds a helper for Listener method such as View.OnKeyListener.onKey(), there are three ways it can go:

1. Listener method returns void: use an EventHandler or EventHandler<T>.

2. Listener method returns bool: use an EventHandler<T>.

3. Listener method returns something else: generate a property, not an event.

In (2), the listener method needs to return a boolean value. The value returned comes from a Handled property that is generated on an EventArgs subclass.

The default value for the Handled property is `true`, on the assumption that if you're subscribing to the event you intend to handle the event. In your case, that's not true: you're subscribing to the event but you're _not_ handling the event, you instead want the default behavior to occur. To do this, you need to set the Handled property to false before your event handler returns.
Comment 3 Uday Gupta 2014-08-16 03:40:42 UTC
I accept your reason to mark it INVALID and explain me clearly.

But when i try

void txtCodeEditor_KeyPressed (object sender, View.KeyEventArgs e)
    //Don't make OnBackPressed working on Back Key press with Down action, otherwise e.Handled=false will hit twice;
    if (e.KeyCode == Keycode.Back && e.Event.Action == KeyEventActions.Down && e.Event.Source == InputSourceType.Keyboard) {
        System.Diagnostics.Debug.WriteLine ("Setting e.Handled");
        e.Handled = true;
    e.Handled = false;

This still blocks the OnBackPressed