[Android] Request event support back button press inside navigation bar in material design (v7)

In Android, like iOS, i use back button press for synchronize view - viewModel.
In Android, i use FormsApplicationActivity class and i override OnOptionsItemSelected method for calling physical method OnBackPressed.
Then inside ContentPage override, i detect back pressed and i can :
-Prevent close view by make a question
-Block closing view

When i change an android application : FormsApplicationActivity to FormsAppCompatActivity. this possibility disappear.
In fact, with FormsAppCompatActivity your renderer create Android ToolBar with FormsAppCompatActivity.ToolbarResource.
I never have possibility to intercept back click on this toolbar because your code call directly popAsync page...

Can you add possibility call OnOptionsItemSelected for intercept it and prevent closing page?

Posts

  • nadjibnadjib DZMember ✭✭✭✭
    edited June 2016

    +1

    I too want to intercept Toolbar's back button on Android and can't find how to do it in FormsAppCompatActivity.

  • fortinguillaumefortinguillaume FRBeta ✭✭

    i develop this functionnality and submit github pull request.
    https://github.com/xamarin/Xamarin.Forms/pull/230

  • Lewis_tLewis_t GBUniversity ✭✭

    I hope this change/or similar gets incorporated. Could it be linked to the bugzilla: https://bugzilla.xamarin.com/show_bug.cgi?id=38189

  • fortinguillaumefortinguillaume FRBeta ✭✭

    Yes it's the same bug

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭
    edited August 2016

    @fortinguillaume , @Lewis_t
    Implements

    protected override void OnCreate(Bundle bundle)
    {
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
    
            //This implementation is necessary
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
        }
    
  • @Dionis.Oliveira

    Do you have a full example project?

    my toolbar ressource is null.

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭
    edited August 2016

    @fortinguillaume You need add the code after - base.OnCreate(bundle); -

    Like

    public class MainActivity : FormsAppCompatActivity
    {
    protected override void OnCreate(Bundle bundle)
    {
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
    
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
        }
    
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
    
            var navigation = App.Current.MainPage.Navigation.NavigationStack;
            if (App.Current.MainPage.Navigation.NavigationStack.Count > 0 && item.ItemId == 16908332 && (navigation[navigation.Count - 1] is ICentroInicioPage))
            {
    

    // you Code
    return false;
    }
    else
    {
    return base.OnOptionsItemSelected(item);
    }
    }
    }

  • thanks, and the style file for your Resource.Id.toolbar?

  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭

    @fortinguillaume

    are you able to get around this issue?

    I have same issue, you can comment here

  • @DionisOliveira said:
    @fortinguillaume You need add the code after - base.OnCreate(bundle); -

    Like

    public class MainActivity : FormsAppCompatActivity
    {
    protected override void OnCreate(Bundle bundle)
    {
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
    
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
        }
    
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
    
            var navigation = App.Current.MainPage.Navigation.NavigationStack;
            if (App.Current.MainPage.Navigation.NavigationStack.Count > 0 && item.ItemId == 16908332 && (navigation[navigation.Count - 1] is ICentroInicioPage))
            {
    

    // you Code
    return false;
    }
    else
    {
    return base.OnOptionsItemSelected(item);
    }
    }
    }

    Your sample work fine :). Very thanks

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭
    edited September 2016

    God Job!

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭

    good job!

Sign In or Register to comment.