How to remove previous pages from memory?

I'm using an implementation of Master/Detail in my Forms app. The problem is that even after I change pages the previous one is still running, I even had to do some inactive/isActive code in order to stop the code execution of those pages.

This is how I change pages:

        public void GoTo(Page page)
        {
            Detail = new NavigationPage(page);
            IsPresented = false;
            UserDialogs.Instance.HideLoading();
        }

Then I do:

Application.Current.HomePage.GoTo(new ServiceSelectionPage());

But the previous page is kept running. What am I doing wrong? Or how do I remove those pages from memory? I tried setting Detail = null to no avail.

Thanks!

Posts

  • ErosSteinErosStein BRMember

    @JulianPasque Does it work with MasterDetail pages? I mean, since I'm just replacing Detail's content.

    Thanks.

  • JulianPasqueJulianPasque USMember ✭✭✭

    Can you try:
    this.Navigation.RemovePage(Detail);
    I never really worked with master/detail. But I guess this should work.

  • ErosSteinErosStein BRMember

    @JulianPasque No, it looks like for this work, I need to use navigation.push or whatever =/

    Thanks.

  • JulianPasqueJulianPasque USMember ✭✭✭

    Navigation.popasync Removes the last page from stack

  • ErosSteinErosStein BRMember

    Do you mean Detail.Navigation.PopAsync()? Because using the navigation property it doesn't work. I'm running some tests to check if it's working with Detail.Navigation.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    What do you mean by "still running"?

  • ErosSteinErosStein BRMember

    @adamkemp for example, I have a while loop on one page, even after I added the new one the while loop keeps running. And I know that because it's a loop that makes a web request to my local server with a breakpoint in it =/

    Thanks.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Well how are you telling the loop to stop? There's no way for Forms to know you're running that loop. It can't stop it for you.

  • ErosSteinErosStein BRMember

    @adamkemp I know that, right now I created a flag for that, but I thought it would stop automatically after I changed pages. The previous page (the one with the loop) should've been removed from memory, but it was not, hence my question here.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    That's not how memory management works in either C# or Objective-C. In fact, there is no language that could do that without crashing. If you have running code that is using an object (a Page in this case) then that object cannot leave memory. The running code either holds the object in memory (as is the case with C#, Objective-C, and Java) or the app crashes when the object leaves memory (likely in C/C++). If you want the code to stop then you have to stop it yourself, and if you want the object to leave memory then at the very least you must stop that code from running. Otherwise the object is considered still in use and stays in memory.

    In other words, you have the causality backwards. It's not the object being in memory that keeps the code running. It's the running code that keeps the object in memory.

  • ErosSteinErosStein BRMember

    Uhm... ok!
    So even after nulling the object the code keeps running... Well, shame on me for not knowing that!

    Alright then, since this is the way, that's what I'm already doing, thank you.

  • Johan_SchoemanJohan_Schoeman ZAMember ✭✭
    edited September 2015

    I use this to remove a page lower down in the stack on iOS:

      public class PageHelper
        {
            private Page _currentPage;
    
            public PageHelper(Page page)
            {
                _currentPage = page;
            }
    
    
            public CapturePage GetSettingsPage()
            {
                SettingPage settingPage = null;
    
                foreach (Page page in _currentPage.Navigation.NavigationStack)
                {
                    if (page.Title == "Setup")
                    {
                        settingPage = (SettingPage)page;
                        break;
                    }
                }
                return settingPage;
            }
    }
    
Sign In or Register to comment.