Xamarin.forms: [Bug] [Android] MainPage change not apply while app is in background

Created on 11 Sep 2019  ·  20Comments  ·  Source: xamarin/Xamarin.Forms

Description

Changing App MainPage while device is locked (app in sleep mode) does not apply the change.

Our production app is launch with an AutoLauncher at the boot of devices, so with device locked most of the time, then our app not be properly initialize (stay on the first MainPage set - i.e the loading page).
I'll even try to wait the AppResume before set MainPage, this does not work either.

Steps to Reproduce

With the repro project attached.
First scenario :

  1. While device locked
  2. Launch the app with vs in debug
  3. Wait for the console output "Information : ChangePage to MainPage"
  4. Unlock your device => App display a blank screen

Second scenario :

  1. While device unlock
  2. Launch the app
  3. When the loading page is display lock your phone (or put the app in background with circle/square button)
  4. Wait 10 sec before unlocking him (or resume the app)
  5. App display the loading page

You can check the also the InitializeApplicationWithAwareOfSleepingStateAsync by uncomment the line in OnStartMethod and comment the InitializeApplicationAsync. That use a TaskCompletionSource to wait the awakening of the app before apply the MainPage (but with the same behavior).

Expected Behavior

App MainPage updated correctly

Actual Behavior

App MainPage does not updated

Basic Information

  • Version with issue: 4.0 and latter
  • Last known good version: 3.6.0.709228
  • IDE: VS 16.2.3
  • Platform Target Frameworks:

    • Android: 9.4.1.0

  • Android Support Library Version: API 28

Reproduction Link

TestNavigateMainPage.zip

4.0.0 regression Android bug

Most helpful comment

@kvpt Working great with the last 4.4.0 released ! Thanks

All 20 comments

Probably link to #7400 . @kvpt can you confirm ?

I'm not sure to understand how to test your reproduction.
The reproduction steps are clear but what is the expected behavior ?

The simplest is that you test yourself with my patched Xamarin.Forms version, to see if it's better or not with it.

Here the project with the patch included : repro7478.zip

I'm not sure to understand how to test your reproduction.
The reproduction steps are clear but what is the expected behavior ?

The simplest is that you test yourself with my patched Xamarin.Forms version, to see if it's better or not with it.

Here the project with the patch included : repro7478.zip

The expected behavior is like in XF 3.6.X, when we set a Page to the MainPage property of the App, see this Page properly set and displayed (no matter the state of the app - sleep or resume). So with the attached repro project, the final page displayed must always be the "Welcome to the MainPage".

With your patch it seems to work correctly (i.e like XF 3.6) !

Just for myself, where i can find your patch in the zip ? where is the file that contains it ?

This zip version contains your repro app and the full source code of the framework with the patch.
It simpler to do this for debug and test.

For the patch only it's the PR #7477.

i've made a mistake, your patch does not work for my issue. I'll will investigate a bit more why the scenario was different with the Master branch and launch directly in debug mode than with the XF 4.2.0.

With the master branch, it's the second scenario only that doesn't work.

We have same issue after updating Xamarin Forms. Cannot set MainPage in background.

Almost same issue here: https://github.com/xamarin/Xamarin.Forms/issues/7290

Previously I was able to get around the issue by adding a Task Delay, but now OnAppearing is never even called.

Argh! I've been struggling to fix this issue thinking it's my app 🤦‍♂️

Argh! I've been struggling to fix this issue thinking it's my app 🤦‍♂️

We are stuck in X.F 3.6 until this bug was corrected. So sad.

Same here. This is another issue that is causing headaches. https://github.com/xamarin/Xamarin.Forms/issues/7784

This bug has been throwing me for a loop as well. Going to try to implement a work around today.

@joshykautz As a workaround I actually inserted the new main page at the top of the stack and then called PopToRoot. Works as I only use navigationpage as my MainPage in my project.

Still this is quite a issue that is still there even for XF4.3.

@mattiascibien That's clever. I anticipate handling this by setting a global variable that will get checked upon the OnResume event, and that check will set the MainPage. Just another bandaid that should work for now 🙂

@mattiascibien That's clever. I anticipate handling this by setting a global variable that will get checked upon the OnResume event, and that check will set the MainPage. Just another bandaid that should work for now 🙂

@joshykautz Can you tell me more about your workaroud ? On the OnResume of App.cs you set the MainPage put in a global variable ?
I've tested (like in my repro app) to work with a TaskCompletionSource that wait for the awakening of the app to set the main page, but the result is the same.

@kvpt I've made a test with the package XF 4.5.0.187-pre2, who contain your PR #7477, but the issue is not corrected, the problem still exist (for the two scenarios).

You can reproduced it with the same attached project available in the "Reproduction Link".

@Seuleuzeuh Sorry I referenced the wrong PR, there was two issues, first one is fixed by #7477 and the other is fixed by the PR #9257 merged yesterday.
I think you will have to wait this one to be available.
If #9257 doesn't fix it either, I will test your reproduction case.

@kvpt Working great with the last 4.4.0 released ! Thanks

I'm still seeing this issue with Xamarin.Forms 4.4. Is there a workaround available?

@dylanberry in my project I fixed the issue by keeping the screen on using a wakelock and disabiling it in OnAppearing of my first page (working fine in an embedded project where I have only one activity).

Also, in another project I actually insert the new main page at the top of the navigation stack and then called PopToRoot. Works as I only use navigationpage as my MainPage in my project.

I'm still seeing this issue with Xamarin.Forms 4.4. Is there a workaround available?

With the last version of the package ?
Your code is similar to my reproduction project ?

Was this page helpful?
0 / 5 - 0 ratings