Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

[Bug][Android]App does not resume but restart activity after being put in background and re-opened

Open Suprndm opened this issue 5 years ago • 12 comments

Description

On Android, when the app is re opened after being put in background, and only after being freshly installed, the activity is rebuilt from scratch, instead of being resumed. All filled information is lost.

  • Version with issue: 4.5.0.530
  • Last known good version: N/A
  • IDE: Visual studio 2019
  • Android: 9
  • Affected Devices: at least Oppo A9 , Samsung galaxy S7, and Huawei Honor 9

Steps to Reproduce

  1. Install a Xamarin App from store or APK, on Android
  2. Open the application
  3. Put the application in background
  4. Reopen it by tapping its icon

Expected Behavior

  • the app is resumed in its previous states

Actual Behavior

  • the app restarts, slightly reload, and display the initial page.

Basic Information

  • It only occurs after a new installation. If you completely kill the application, and try to reproduce the issue, it will work as expected.
  • You can try reproducing the issue with any xamarin app on the appStore,
  • On apps not using Xamarin, i don't reproduce the issue
  • It only occurs when installed from APK / store. I can't reproduce it from Debug in Dev env.

Suprndm avatar Sep 08 '20 08:09 Suprndm

Same for me, even in Debug on a real device with Android 9. v4.8.0.1451

Killing the app makes it work as expected too.

what3var avatar Sep 27 '20 13:09 what3var

Same issue for me on Android 11.

ChristianTBaker avatar Nov 03 '20 15:11 ChristianTBaker

Same issue on real device under Android 9.0 with Xamarin Forms 4.8.0.1687

App.OnStart is called instead of App.OnResume

emjy86 avatar Nov 19 '20 07:11 emjy86

I have similar issue (not sure if it is same). But here is what I found. I am still in development environment and notice this issue.

  • Start the application, in Emulator from Visual Studio. Go to Any flyout menu page.
  • Press "Back" button in Andriod device.
  • It unload the application in background.
  • Bring app to foreground.
  • It call App constructor and OnStart there after.

But

  • Start App from Debugger,
  • Open any flyout menu page.
  • Press "Home button" to put app in background. (it is same list in android as pressing Back button)
  • Bring app back in foreground.
  • OnResume is called. and app didn't reinitalised.

So Android is putting app in 2 different state which might be causing different behavior when bring back from background.

Also, When I run app by tapping icon or first time it works without problem, but when it come from background to reinitialize it crashes as OnStart function behave differently, ie. in my OnStart I was syncing some data from server in sequence. But when it reinitialize it fail to sync data and cause data to miss their order of syncing causing error in my application.

Expected Behavior is

  1. It should treat bring from background as one. So OnResume is called everytime.
  2. If it is not "resume" then it should initialize completely not partially, (i.e. my FlyOut menu page try to load data before Onstart is finished ).

archergod avatar May 26 '21 05:05 archergod

I am seeing the same behaviour trying to upgrade to Xamarin.Forms (5.0.0.2401):

  • Start App
  • Background app
  • If you use Android recents screen to foreground the app OnResume is called as expected
  • If you use app drawer or home screen icon to launch app OnResume is not called, app ctor is called again (Bad!).

This issue can be easily replicated using the Flyout App template in Visual Studio for Mac. iOS works as expected.

Does anyone have any workarounds? This is a major issue that is blocking us from moving forward.

I really do not see any traction on this from the forms team as it has been open since Sept 2020. @samhouts @StephaneDelcroix can we get an update on this?

pasteusernamehere avatar Apr 07 '22 12:04 pasteusernamehere

Did you find a solution? I have the exact same problem using 5.0.0.2401.

I think I may found the issue here. I do have two Android Activities, SplashActivity and MainActivity. Both were using NoHistory = true. Changing the MainActivity to NoHistory = false seems to have fixed the problem to me.

wagenheimer avatar May 18 '22 17:05 wagenheimer

How is this still an open issue for this long ?

stephenhauck avatar May 18 '22 17:05 stephenhauck

Did you find a solution? I have the exact same problem using 5.0.0.2401.

I think I may found the issue here. I do have two Android Activities, SplashActivity and MainActivity. Both were using NoHistory = true. Changing the MainActivity to NoHistory = false seems to have fixed the problem to me.

Using NoHistory = true is not an option for me. I tried it and had no luck. Regardless I want history.

@samhouts @StephaneDelcroix can we get an update on this?

pasteusernamehere avatar May 19 '22 19:05 pasteusernamehere

Did you find a solution? I have the exact same problem using 5.0.0.2401. I think I may found the issue here. I do have two Android Activities, SplashActivity and MainActivity. Both were using NoHistory = true. Changing the MainActivity to NoHistory = false seems to have fixed the problem to me.

Using NoHistory = true is not an option for me. I tried it and had no luck. Regardless I want history.

@samhouts @StephaneDelcroix can we get an update on this?

I tried setting NoHistory to false, it worked for some reason, using v5.0.0.2401

ViniciusAugustoRR avatar Jun 09 '22 12:06 ViniciusAugustoRR

I sort it out for me by adding

       protected override void OnSleep()
       {
          ... //Your code to save some setting manually goes before OnSleep.
           base.OnSleep(); //This will save the state when sleep is called.
       }

       protected override void OnResume()
       {
           base.OnResume();   //This will restore the state..
          ... my task resume action if any.
       }

I had those function but empty when I create app and without base.OnXXXX() they won't save state. I had problem with both IOS and Android because of this. If you don't have problem in both then it might not be the solution for you.

Edit: the codes goes for App.Xaml.cs in core app.

archergod avatar Jun 09 '22 12:06 archergod

I sort it out for me by adding

       protected override void OnSleep()
       {
          ... //Your code to save some setting manually goes before OnSleep.
           base.OnSleep(); //This will save the state when sleep is called.
       }

       protected override void OnResume()
       {
           base.OnResume();   //This will restore the state..
          ... my task resume action if any.
       }

I had those function but empty when I create app and without base.OnXXXX() they won't save state. I had problem with both IOS and Android because of this. If you don't have problem in both then it might not be the solution for you.

Edit: the codes goes for App.Xaml.cs in core app.

I tried this, app still does not resume to where it left off. Would be nice if @samhouts @StephaneDelcroix would comment on the thread and give an update.

pasteusernamehere avatar Jun 10 '22 12:06 pasteusernamehere

Did you find a solution? I have the exact same problem using 5.0.0.2401.

I think I may found the issue here. I do have two Android Activities, SplashActivity and MainActivity. Both were using NoHistory = true. Changing the MainActivity to NoHistory = false seems to have fixed the problem to me.

Yep this worked for me too!

ruchira-net avatar Sep 12 '22 04:09 ruchira-net

any updates about this?

agustinbishel avatar Dec 20 '22 20:12 agustinbishel

any updates about this?

not from the ppl making it i dont think, but there are some fixes on this very thread

ViniciusAugustoRR avatar Dec 20 '22 20:12 ViniciusAugustoRR

any updates about this?

not from the ppl making it i dont think, but there are some fixes on this very thread

Thank you. It's actually normal behavior within the android ecosystem (I saw it in another applications). The other thing that can be done is to intercept the onbackbuttonpressed and send the application to sleep (im still searching it). The solution of NoHistory isn't working here...

agustinbishel avatar Dec 20 '22 20:12 agustinbishel

any updates about this?

not from the ppl making it i dont think, but there are some fixes on this very thread

Thank you. It's actually normal behavior within the android ecosystem (I saw it in another applications). The other thing that can be done is to intercept the onbackbuttonpressed and send the application to sleep (im still searching it). The solution of NoHistory isn't working here...

are you using xamarin or MAUI, and wich version ?

ViniciusAugustoRR avatar Dec 20 '22 21:12 ViniciusAugustoRR

any updates about this?

not from the ppl making it i dont think, but there are some fixes on this very thread

Thank you. It's actually normal behavior within the android ecosystem (I saw it in another applications). The other thing that can be done is to intercept the onbackbuttonpressed and send the application to sleep (im still searching it). The solution of NoHistory isn't working here...

are you using xamarin or MAUI, and wich version ?

Xamarin 5.0.0.2337

agustinbishel avatar Dec 20 '22 21:12 agustinbishel

any updates about this?

not from the ppl making it i dont think, but there are some fixes on this very thread

Thank you. It's actually normal behavior within the android ecosystem (I saw it in another applications). The other thing that can be done is to intercept the onbackbuttonpressed and send the application to sleep (im still searching it). The solution of NoHistory isn't working here...

are you using xamarin or MAUI, and wich version ?

Xamarin 5.0.0.2337

hmm got it, i have been using MAUI so, that maybe be why it worked for me... i do have the feeling that it worked from me in a version of xamarin, but i will have to dig kind of deep on my repositories lol, will update you if i find it tho, but do consider updating to MAUI if is of urgency to you in this case

ViniciusAugustoRR avatar Dec 20 '22 21:12 ViniciusAugustoRR