OnOptionsItemSelected not triggered in FormsAppCompatActivity(Forms1.5.1+)

According to https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/,I upgrade my App to Material Design.
But the OnOptionsItemSelected method is not triggered when the user was clicking on the back arrow icon.
Can anyone help me ?

Best Answer

Answers

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭

    @SpaceMonkey , @CSNTAndroid .

    In activity ,you need implement the code following, after this implementing, the method worked.

    Good Luck!

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

            base.OnCreate(bundle);
    
            //The implementation
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
        }
    
  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭

    @DionisOliveira

    Still not getting invoke, even after implementing your suggestion.

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭

    @Abhijeet_Surya

    Try in your activity. xamarin.forms 2.3.1.114

    -
    FormsAppCompatActivity.BackPressed += FormsAppCompatActivity_BackPressed;

    https://github.com/xamarin/Xamarin.Forms/pull/230

  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭
    edited September 2016

    @DionisOliveira

    I tried the mention fix in stable version of Forms 2.3.1.114 -

    No success

    Below is the code snippet, am I missing something ? I did this in MainActivity as well as in renderer.

    FormsAppCompatActivity.BackPressed += FormsAppCompatActivity_BackPressed;

    public bool FormsAppCompatActivity_BackPressed(object sender, EventArgs e)
    {
    return true;
    }

  • this fix doesn't work. (FormsAppCompatActivity.BackPressed += FormsAppCompatActivity_BackPressed;)

    I test with success DionisOliveira's solution

    @DionisOliveira said:
    @SpaceMonkey , @CSNTAndroid .

    In activity ,you need implement the code following, after this implementing, the method worked.

    Good Luck!

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

            base.OnCreate(bundle);
    
            //The implementation
            Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
        }
    

    I commit the change to my github project (commit Android Add Back button #44)
    https://github.com/Thetyne/BodyReport.Mobile

  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭

    @fortinguillaume @DionisOliveira

    Thanks, I have created fresh sample app. Able to intercept the back button.

    (earlier I was ignoring Values-v21)

    But when I tried same solution in my existing app, toolbar is always null. (This is almost same as sample app only with lots of plugins & IOC)


    Toolbar toolbar = FindViewById(Resource.Id.toolbar); ---This always returns null :-(
    SetSupportActionBar(toolbar);

    What could be probably wrong in my existing application?

  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭

    Thanks, @fortinguillaume @DionisOliveira

    I am able to resolve it.

    I added code to OnPostCreate


    protected override void OnPostCreate(Bundle savedInstanceState)
    {
    Android.Support.V7.Widget.Toolbar toolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    SetSupportActionBar(toolBar);
    base.OnPostCreate(savedInstanceState);
    }

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭
    edited September 2016

    God Job!

  • DionisOliveiraDionisOliveira USDeveloper Group Leader ✭✭

    good job!

  • Fandy_AdamFandy_Adam USMember

    Hi Guys,

    First i only use SetSupportActionBar like suggested in this forum, then i try other solution i got from here : https://github.com/xamarin/Xamarin.Forms/pull/235

    Here some code i add to OnPostCreate :
    Toolbar toolbar = this.FindViewById(Resource.Id.toolbar);
    SetSupportActionBar(toolbar);
    SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    SupportActionBar.SetHomeButtonEnabled(true);
    SupportActionBar.Title = "Hello From Toolbar";

    Unfortunately my Xamarin Forms Apps, still not firing the OnOptionItemSelected event. Beside that, the Title is not changing to "Hello From Toolbar also".

    Any other suggestion ?

  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭

    @fandyadam
    Which version of android you are testing ?
    If it is above 21 then you need to have Values-v21 folder for style.
    Also make sure to define
    using Toolbar = Android.Support.V7.Widget.Toolbar;

    Let me know if it works or not.

  • Fandy_AdamFandy_Adam USMember

    Hi Abhijeet,

    The version of android i'm testing in is android lollypop and marshmallow.

    Yes, i'm already using values-21, this is the code :

    #style name="TestTheme.Splash" parent="android:Theme.Material.Light.NoActionBar.Fullscreen"#
    #item# name="android:colorBackground"#@color/splash #/item#
    #/style#

    Change # to <>

    For my Main Activity :
    [Activity(Label = "MainActivity", Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait, Theme = "@style/TestTheme.Base")]
    public class MainActivity : FormsAppCompatActivity
    {
    protected override void OnCreate(Bundle bundle)
    {
    //##1.Init Default Layout
    ToolbarResource = Resource.Layout.toolbar;
    TabLayoutResource = Resource.Layout.tabs;

            //##2.Start Things
            base.OnCreate(bundle);
    
            //##3.Init Plugin
            //---CachedImageRenderer
            CachedImageRenderer.Init();
    
            //---UserDialogs
            UserDialogs.Init(this);
    
            //---Image Circle Renderer
            ImageCircleRenderer.Init();
    
            //##4.Init Xamarin
            global::Xamarin.Forms.Forms.Init(this, bundle); 
    
            //##6.Load Application
            LoadApplication(new App());
        }
    
        protected override void OnPostCreate(Bundle savedInstanceState)
        {
            Android.Support.V7.Widget.Toolbar toolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    
            SetSupportActionBar(toolBar);
    
            base.OnPostCreate(savedInstanceState);
        }
    
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            System.Diagnostics.Debug.WriteLine("OnOptionsItemSelected");
            return base.OnOptionsItemSelected(item);
        }
    }
    

    For information, I'm playing Splash Activity first before continuing to the MainActivity, here the SplashActivity Code :
    [Activity(MainLauncher = true, NoHistory = true, ScreenOrientation = ScreenOrientation.Portrait, Theme = "@style/TestTheme.Splash")]
    public class SplashActivity : Activity
    {
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            SetContentView(Resource.Layout.splash);
        }
    
        protected override void OnResume()
        {
            base.OnResume();
    
            Task startupWork = new Task(() => {
                Task.Delay(50);  // Simulate a bit of startup work.
            });
    
            startupWork.ContinueWith(t => {
                StartActivity(new Intent(Application.Context, typeof(MainActivity)));
            }, TaskScheduler.FromCurrentSynchronizationContext());
    
            startupWork.Start();
        }
    }
    

    Thanks for your help Abhijeet :smile:

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @Abhijeet_Surya Thanks for the solution!

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @Abhijeet_Surya Actually this removes my toolbar items the first time I land to the page, but then when I navigate back or to another page then they are displayed, any thoughts?

Sign In or Register to comment.