Venky
Venky

Reputation: 2047

how to make the tabbed page navigate when clicked on the same tab text/icon twice?

So i have a tabbed page, and there are several tabs associated with it enter image description here

on the first tab i have a list, in which if i click a tile, lets say tile1, then i open up another page keeping the tabs intact, but what i want to is when i click the tab option1 again, then it should go to the main list, which is not happening any insight would be really helpful Clicking on "tile1", i do

await Navigation.PushAsync(new SubCategoryView());

which i assume is the correct way, and ironically enough intended behaviour works in iOS but not on Android

Upvotes: 0

Views: 658

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

If you want to return to the root page when select the same tabbed , you can use Custom Renderer

in Android


using Android.Content;

using Android.Support.Design.Widget;
using xxx;
using xxx.Droid;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedRenderer))]
namespace xxx.Droid
{
    public class MyTabbedRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
    {

        public MyTabbedRenderer(Context context) : base(context)
        {

        }

        private TabbedPage tabbed;
        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                tabbed = (TabbedPage)e.NewElement;
            }
            else
            {
                tabbed = (TabbedPage)e.OldElement;
            }

        }
        async void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
        {
            await tabbed.CurrentPage.Navigation.PopToRootAsync();
        }

    }

   
}

in iOS

using UIKit;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using xxx;
using xxx.iOS;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedRenderer))]
namespace xxx.iOS
{
    public class MyTabbedRenderer : TabbedRenderer
    {
        private TabbedPage tabbed;
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                tabbed = (TabbedPage)e.NewElement;
            }
            else
            {
                tabbed = (TabbedPage)e.OldElement;
            }

            try
            {
                var tabbarController = (UITabBarController)this.ViewController;
                if (null != tabbarController)
                {
                    tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private async void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs)
        {
            if (tabbed?.CurrentPage?.Navigation != null && tabbed.CurrentPage.Navigation.NavigationStack.Count > 0)
            {
                await tabbed.CurrentPage.Navigation.PopToRootAsync();
            }

        }
    }
}

Upvotes: 1

Related Questions