Bug 59882 - Xam Forms 2.4 not resuming from reused App and Pages
Summary: Xam Forms 2.4 not resuming from reused App and Pages
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.4.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-10-02 06:39 UTC by Shane
Modified: 2017-12-07 20:44 UTC (History)
7 users (show)

Tags: ac fr
Is this bug a regression?: Yes
Last known good build:

recreate resume differences with 2.3.4 vs 2.4.0 (974.76 KB, application/x-zip-compressed)
2017-10-02 06:39 UTC, Shane

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:

Description Shane 2017-10-02 06:39:44 UTC
Created attachment 25048 [details]
recreate resume differences with 2.3.4 vs 2.4.0

After upgrading to Xam Forms 2.4 on Android if I background my app and then foreground it my app doesn't reload. The best way I've found to recreate this behavior is to hit the back button out of the app so the Main Activity is destroyed. Currently my App and Pages are stored as singletons that I reuse which has worked fine for me in the past but doesn't seem to anymore. Maybe that's no longer allowed or I was lucky it ever did? The only way I've found to make it work is by recreating the App and Pages.  I've attached a sample app with one version using Xam forms 2.3.4 and then other is using 2.4.0. 

Run the app
Hit the back button
Restart the app

On the 2.3.4 version it'll load back up whereas on 2.4.0 it doesn't
Comment 1 Shane 2017-10-02 06:40:35 UTC
Microsoft Visual Studio Enterprise 2017 
Version 15.3.5
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Visual Basic 2017   00369-60000-00001-AA951
Microsoft Visual Basic 2017

Visual C# 2017   00369-60000-00001-AA951
Microsoft Visual C# 2017

Visual C++ 2017   00369-60000-00001-AA951
Microsoft Visual C++ 2017

Visual F# 4.1   00369-60000-00001-AA951
Microsoft Visual F# 4.1

ASP.NET and Web Tools 2017   15.0.30726.0
ASP.NET and Web Tools 2017

ASP.NET Core Razor Language Services   1.0
Provides languages services for ASP.NET Core Razor.

ASP.NET Template Engine 2017   15.0.30726.0
ASP.NET Template Engine 2017

ASP.NET Web Frameworks and Tools 2017   5.2.50601.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   15.0.30728.0
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.3.0000.0
Microsoft Azure Data Lake Tools for Visual Studio

Azure Data Lake Tools for Visual Studio   2.3.0000.0
Microsoft Azure Data Lake Tools for Visual Studio

Command Task Runner   1.2.34
Adds support for command line batch files in Visual Studio 2015's Task Runner Explorer. Supports .cmd, .bat, .ps1 and .psm1 files.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

JavaScript UWP Project System   2.0
JavaScript UWP Project System

JetBrains ReSharper Ultimate 2017.2.1    Build 109.0.20170920.170950
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2017 JetBrains, Inc.

JustDecompilePackage2017 Extension   1.0
JustDecompilePackage2017 Visual Studio Extension Detailed Info

Markdown Editor   1.11.214
A full featured Markdown editor with live preview and syntax highlighting. Supports GitHub flavored Markdown.

Merq   1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft Azure HDInsight Azure Node   2.2.8000.0
HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service   2.3.0000.0
Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio   1.7
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service   2.3.0000.0
Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node   1.0
Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.50719.1

Microsoft Continuous Delivery Tools for Visual Studio   0.3
Simplifying the configuration of continuous build integration and continuous build delivery from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   4.6.8-pre (ec7034f)
Support for debugging Mono processes with Visual Studio.

Continuous Testing Tool for .NET
Copyright © 2010-2017 Remco Software Ltd

NPM Task Runner   1.4.81
Adds support for npm scripts defined in package.json directly in Visual Studio's Task Runner Explorer. Includes full support for Yarn

NuGet Package Manager   4.3.1
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

OptionsPackage Extension   1.0
OptionsPackage Visual Studio Extension Detailed Info

Project   1.0
Information about my package

Redgate ReadyRoll
Extend DevOps processes to your SQL Server databases and safely automate database deployments.
Visit https://www.red-gate.com/readyroll for more information.

Copyright (C) 2011 Red Gate Software Ltd. All rights reserved.
This software contains components from Component Owl.
SQL Server is a registered trademark of Microsoft Corporation. 
Visual Studio is a registered trademark of Microsoft Corporation. 

ReadyRoll contains code from the following open source software:

NuGet https://www.nuget.org/
SQL LocalDB Wrapper https://github.com/martincostello/sqllocaldb
Autofac https://autofac.org/
Json.NET https://json.net/
MahApps.Metro http://mahapps.com/
SemVer https://github.com/maxhauser/semver
Log4Net http://logging.apache.org/log4net/
Extended WPF Toolkit https://wpftoolkit.codeplex.com/
Code InfoBox VSX http://www.codeproject.com/Articles/55196/Code-InfoBox-Visual-Studio-Extension-VSX
OctoPack https://github.com/OctopusDeploy/OctoPack
SQLite https://sqlite.org/

This product contains icons from http://www.visualpharm.com distributed under a free backlink license.

For license details or other notices relating to the above software, please see NOTICE.TXT and EULA.rtf in the ReadyRoll application folder.

Redgate SQL Prompt
Write, format, and refactor SQL effortlessly

SQL Server Analysis Services   14.0.806.134
Microsoft SQL Server Analysis Services Designer 
Version 14.0.806.134

SQL Server Data Tools   15.1.61707.200
Microsoft SQL Server Data Tools

SQL Server Reporting Services   14.0.806.134
Microsoft SQL Server Reporting Services Designers 
Version 14.0.806.134

SsmsVsIntegration   1.0
Information about my package

TargetFrameworkMigrator   1.0
Bulk Change .Net Projects Target Frameworks

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript tools for Visual Studio

Typewriter   1.14.0
Typewriter generates TypeScript files from c# code files using TypeScript Templates. This allows you to create fully typed TypeScript representations of server side API that automatically updates when you make changes to your c# code.

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for Unity
Visual Studio Tools for Unity

Visual Studio Tools for Universal Windows Apps   15.0.26730.08
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

WebJobs Tools v1.0.0   15.0.30923.0
WebJobs Tools v1.0.0

WebPack Task Runner   1.5.91
Adds support for WebPack in Visual Studio 2015's Task Runner Explorer.

Xamarin (bd7e3753c)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK (fb018c5)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK (152b654)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 2 Paul DiPietro [MSFT] 2017-10-02 15:12:35 UTC
Is there any reason you aren't using the standard behavior like this?

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;


            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new XamFormsNew.App());

For now you could probably call this a pseudo-regression, but it would otherwise need to be investigated to see if it was supposed to work in the first place.
Comment 3 Shane 2017-10-02 16:45:09 UTC
TBH I don't quite remember that decision beyond saying that I used
as one of my first reference projects

I'd say the motivation for the static app was just the idea that it was already new'd up... The performance hit of doing that was already paid so might as well reuse it.

The part of this that's annoying is that it's not just the reuse of the App.xaml that causes the issues it's the reuse of the pages as well ... So if I take my example and just new up the App but then retain the Pages I get the same issue. 

So for example this doesn't work anymore
        private static NavigationPage navPage = null;
        private static MainPage mainPage = null;

        public App()
            if (mainPage == null)
                mainPage = new MainPage();
                navPage = new NavigationPage(mainPage);

            MainPage = navPage;

Meaning now I have a NavigationPage still in memory that's useless so I have to new up a fresh one and rebuild the stack

I liked using pages already in memory because the first time you use pages in Xam Forms there's always a performance hiccup, also they are already there in memory so I'm less worried about memory leaks.  Now I have an application that still has parts of itself stored in memory but they are non functional so I have to swap them out, make sure all bonds are severed, and rebuild the navigation stack.  Whereas before it was just a matter of hey here's a nav page with pages that are already all wired up to VMs have at it.
Comment 4 Chris King 2017-10-13 19:46:35 UTC
Customer expected MainPage could be re-initialized after reloading app. Actually, existence of a renderer for the MainPage prevented MainPage from being attached the the new activity. This is "correct" (although still a regression) as the old MainPage renderer existed in a different context. 

The "fix" is to clear the MainPage renderer so a new renderer is created in the correct context. I say, "fix" because it does not reuse the existing renderer so the customer will not see the same performance benefit however it'll likely have saved the customer from even more insidious bugs stemming from using views from a deleted context.
Comment 6 Samantha Houts [MSFT] 2017-10-16 18:53:03 UTC
marking in progress since there is an open PR.
Comment 7 Rui Marinho 2017-10-18 15:02:50 UTC
Should be fixed on 2.4.0 service release 2
Comment 8 Shane 2017-10-18 15:04:40 UTC

Thank you for the quick turnaround
Comment 9 Samantha Houts [MSFT] 2017-10-20 00:33:24 UTC
Comment 10 Almando 2017-10-23 20:00:39 UTC
This check in does not check for NULL MainPage and causes an application that creates the page during OnStart() vs new App() to experience a very difficult to trace NRE.