Home > Back-end >  How to go back to previous page of TabbedPage when closing Popup in Xamarin
How to go back to previous page of TabbedPage when closing Popup in Xamarin

Time:03-07

I'm learning more about TabbedPage. I have a problem that I hope someone can help me with:

I have pages of TabbedPage (Page1, Page2, Page3, Page4, Page5). When I select Page5 of TabbedPage it shows Popup. When I close the Popup how can it go back to the previous Page I touched of the TabbedPage.

For example: I am selecting Page2 TabbedPage, next I choose Page5 TabbedPage, it shows Popup, I close Popup, it will go back to select Page2 of TabbedPage, similar: I am selecting Page3 TabbedPage, next I choose Page5 TabbedPage , it shows Popup, I close Popup, it will go back to select Page3 TabbedPage,.....

Note that it will go back to the previously selected TabbedPage.

MainView.xaml

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            BackgroundColor="#fff".....>
    <!--Pages can be added as references or inline-->
    <views:Page1 Title="Page 1" IconImageSource="homeicon" BackgroundColor="#f7f7f7"/>
    <views:Page2 Title="Page 2" IconImageSource="manageorder" BackgroundColor="#fff"/>
    <views:Page3 Title="Page 3" IconImageSource="feeds" BackgroundColor="#fff"/>
    <views:Page4 Title="Page 4" IconImageSource="moneys" BackgroundColor="#fff"/>
    <views:Page5 Title="Page 5" IconImageSource="accounticon" BackgroundColor="#fff"/>
</TabbedPage>

MainView.xaml.cs

private async void PopupAlert()
{
    await PopupNavigation.Instance.PushAsync(new PopupAlertPage());
}

protected override void OnCurrentPageChanged()
{
    base.OnCurrentPageChanged();
    if (CurrentPage is Page5)
    {
        PopupAlert();
    }
}

PopupAlertPage.xaml.cs

private void close_Tapped(object sender, EventArgs e)
{

    //App.Current.MainPage = new NavigationPage(new MainView(0));
    PopupNavigation.Instance.PopAllAsync();
    Application.Current.MainPage.Navigation.PopAsync();
}

Ask for help. Thanks very much

Update1

MainView.xaml.cs

public partial class MainView
{
    public MainView(int index)
    {
        NavigationPage.SetHasNavigationBar(this, false);
        InitializeComponent();
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        //CurrentPage = Children[0];
        
        SetPage(index);
    }
    void SetPage(int index)
    {
        CurrentPage = Children[index];
    }
    private async void PopupAlertLogin()
    {
        await PopupNavigation.Instance.PushAsync(new PopupAlertLogin());
    }

    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();
        if (CurrentPage is Page5)
        {
            PopupAlertLogin();
        }
    }
}

PopupAlertPage.xaml.cs

private void close_Tapped(object sender, EventArgs e)
{
    PopupNavigation.Instance.PopAllAsync();

    var tab = (TabbedPage)Application.Current.MainPage;
    tab.CurrentPage = tab.Children[0];

    //var mainPage = this.Parent as TabbedPage;
    //mainPage.CurrentPage = mainPage.Children[4];
}

Error:

enter image description here

CodePudding user response:

Even though i don't support your logic but you can achieve that through this way.

In your MainView Class, Declare a property OldPageIndex and add this function.

 public static int OldPageIndex { get; set; }

 protected override async void OnCurrentPageChanged()
        {
            
            base.OnCurrentPageChanged();
            
            if (this.CurrentPage is Page5)
            {
              //await Navigation.PushAsync(new popup());
               // Navigate to any page you want . 
               //PopupAlertLogin
            }
            else
            {
                OldPageIndex = this.CurrentPage.TabIndex;
            }
        }

On the Page you navigated PopupAlertLogin to On Close Funtion.

private void close_Tapped(object sender, EventArgs e)
        {
            var tab = (App.Current.MainPage as NavigationPage).RootPage as TabbedPage;
            tab.CurrentPage = tab.Children.FirstOrDefault(c => c.TabIndex == MainView.OldPageIndex);
            PopupNavigation.Instance.PopAllAsync();
        }

CodePudding user response:

You could use the Children.IndexOf(CurrentPage) to get the tab index of TabbedPage. And then use the MessageCenter to navigate to the page with specific index.

TabbedPage1:

public partial class TabbedPage1 : Xamarin.Forms.TabbedPage
{
    public static int index { get; set; }     
    public TabbedPage1()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
        InitializeComponent();
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        //CurrentPage = Children[0];
        MessagingCenter.Subscribe<Object, int>(this, "Navigation", (arg, idx) =>
        {
            // idx: the index of pages in tabbed that you want to navigate
            CurrentPage = this.Children[idx];

        });
    }
    private async void PopupAlert()
    {
        await PopupNavigation.Instance.PushAsync(new PopupAlertLogin());

    }
    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();

        if (CurrentPage is Page5)
        {
            PopupAlert();
        }
        else
        {
            index = Children.IndexOf(CurrentPage);

        }
    }
}

PopupAlertLogin:

    private void close_Tapped(object sender, EventArgs e)
    {                  
        MessagingCenter.Send<object, int>(this, "Navigation", TabbedPage1.index);// if you want to navigation to the specific page .
        PopupNavigation.Instance.PopAllAsync();
    }
  • Related