I have created a Toolbar for an activity in Android in Xamarin. I have enabled the back/home button with SupportActionBar.SetDisplayHomeAsUpEnabled(true);
. I am trying to capture the event of pressing the back/home button with the following code, as instructed by this and many other stackoverflow posts:
public override bool OnOptionsItemSelected(IMenuItem item)
{
System.Diagnostics.Debug.WriteLine("OnOptionsItemSelected() called: " + item.ItemId);
switch (item.ItemId)
{
case Resource.Id.home:
System.Diagnostics.Debug.WriteLine("Home button pressed");
Finish();
return base.OnOptionsItemSelected(item);
default:
return base.OnOptionsItemSelected(item);
}
}
When I press the back button, OnOptionsItemSelected
is called, but item.ItemId
is not equal to Resource.Id.home
. The former is 16908332 (tested on two different devices) but the latter is 2131492903. How can I capture the home/back button from the toolbar in Xamarin? One possible option is to hardcode the back button ID as 16908332, but I do not know if that number will stay the same permanently.