Xamarin.Forms
Xamarin.Forms copied to clipboard
[Bug][Android]App does not resume but restart activity after being put in background and re-opened
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
- Install a Xamarin App from store or APK, on Android
- Open the application
- Put the application in background
- 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.
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.
Same issue for me on Android 11.
Same issue on real device under Android 9.0 with Xamarin Forms 4.8.0.1687
App.OnStart is called instead of App.OnResume
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
- It should treat bring from background as one. So OnResume is called everytime.
- 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 ).
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?
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.
How is this still an open issue for this long ?
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?
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
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 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.csin 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.
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!
any updates about this?
any updates about this?
not from the ppl making it i dont think, but there are some fixes on this very thread
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...
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 ?
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
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