Bug 57716 - Toolbar not displayed if page cached and re-used
Summary: Toolbar not displayed if page cached and re-used
Status: CLOSED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-06-23 15:13 UTC by John Hardman
Modified: 2017-09-05 15:11 UTC (History)
4 users (show)

Tags: Toolbar WeakReference
Is this bug a regression?: ---
Last known good build:


Attachments
sample project (296.23 KB, application/x-zip-compressed)
2017-06-26 17:53 UTC, Jimmy [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
CLOSED INVALID

Description John Hardman 2017-06-23 15:13:06 UTC
For a page that will get a lot of use, I kept a WeakReference to the page object and used that when pushing the page onto the navigation stack on 2nd and subsequent uses. I had disabled that code long ago as it resulted in problems, but people in the forums had said recently that they were doing that successfully, so I re-enabled it. Well, it does still cause problems, so I'll be disabling it again. In the meantime, here's how to reproduce using the code below.

Push an instance of Bug54977ReproRootPageView onto the navigation stack.
Press the button.
Check for the toolbar (it is visible).
Hit back.
Press the button again.
Check for the toolbar (it isn't visible).

Whilst the toolbar could be re-added in OnAppearing, my concern would be as to what else might be going wrong that isn't immediately apparent, so this use of WeakReference will be disabled in my code again.

Note that this isn't just on Android. It occurs on iOS and UWP as well.


using System;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace ViewsUsingXamarinForms
{
    public class Bug54977ReproRootPageView : ContentPage
    {
        private static WeakReference bug54977ReproPageView;

        public Bug54977ReproRootPageView()
        {
            Title = "Bug54977 Page 1";
            Content = new Button
            {
                Text = "Press me",
                Command = new Command(async () => { await OnClickAsync(); }),
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center
            };
        }

        private Task OnClickAsync()
        {
            Bug54977ReproPageView page
                = bug54977ReproPageView?.Target as Bug54977ReproPageView;

            if (page == null)
            {
                page = new Bug54977ReproPageView();
                bug54977ReproPageView = new WeakReference(page);
            }

            return Navigation.PushAsync(page);
        }

    } // class

    internal class Bug54977ReproPageView : ContentPage
    {
        internal Bug54977ReproPageView()
        {
            this.Title = "Bug54977 Page 2";

            Content = new Label
            {
                Text = "Check for toolbar item above, then hit Back",
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                LineBreakMode = LineBreakMode.WordWrap,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center
            };

            ToolbarItems.Add(new ToolbarItem
            {
                Order = ToolbarItemOrder.Primary,
                Priority = 0,
                Text = "Value",
            });
        }

    } // class

} // namespace ViewsUsingXamarinForms

// eof
Comment 1 Jimmy [MSFT] 2017-06-26 17:53:23 UTC
Created attachment 23120 [details]
sample project

I ran the code provided but I was not able to reproduce the issue. I tested on Android, iOS, and UWP with both 2.3.4.274 and 2.3.5-pre5 and the ToolbarItem was always present on the second page.

In the previous report you mentioned that the issue was intermittent, so I tested pushing the second page around 10 times on each platform just to be sure. How often are you seeing the issue? Can you run the attached project to see if it repros on your machine? Thanks!
Comment 2 Paul DiPietro [MSFT] 2017-07-31 12:54:06 UTC
As we haven't received a response for 30 days, this will be closed as it is assumed the issue is no longer reproducible. If the issue is still occurring with the latest pre or nightly build, please reopen this with a reproduction project we can use for further investigation. Thanks!
Comment 3 John Hardman 2017-09-05 15:10:03 UTC
Sorry - this turned out to be my problem. I'd forgotten that my subclass of NavigationPage cleaned up toolbar items, along with behaviors and binding context. I've added an interface now that identifies pages that the automatic cleanup should ignore, so that those pages can be cached using WeakReference and re-used.