Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XamarinForms AppCompat OnOptionsItemSelected

I have recently updated xamarin forms to 1.5.1-pre1 so that I can use the beautiful AppCompat themes. It works and looks very nice.

I do have one problem, in my old FormsApplicationActivity I used to override the OnOptionsItemSelected method to intercept when the user was clicking on the back arrow icon and do some viewmodel cleanup. Apparently this method is not being called after using the FormsAppCompatActivity. How can I intercept the "soft" back button press (toolbar icon not hard back button) ?

I also tried to override the Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer but i can't seem to override it :(

Does anyone have a clue how I can intercept this?

like image 549
xabre Avatar asked Feb 08 '26 03:02

xabre


1 Answers

You can add the following to your custom renderer. You can either user current activity plugin or cast your context to activity.

var toolbar = CrossCurrentActivity.Current?.Activity?.FindViewById<Toolbar>(Resource.Id.toolbar);

toolbar.NavigationClick += ToolbarNavigationClick;
like image 78
Rohit Vipin Mathews Avatar answered Feb 12 '26 03:02

Rohit Vipin Mathews