Wrong stack behavior on first application run (before restart) - android

I am trying to understand some behavior of the android apps, now that I faced the following issue :
After device restart my app launches by default, but with one issue : If I put it in the background, and I try to resume it from HOME launcher, it restarts from splash
Weird is that this happens ONLY & ONLY after the device it was installed on, is restarted ! To break this behavior I have to kill the app and launch it again. From that point it works as expected -> If in background, using launcher icon restore the app state
Looking a bit in the logs from lifecycles of the activities in cause, I noticed that the flow is pretty much the same ! With the difference that after onResume of last active activity, the SplashScreen gets launched (this being LAUNCHER activity)
Now, I've seen that this might be already an issue reported to Google, even multiple times, but .. If I cannot fix or workaround it, I am at least trying to understand the flow of what happens on these cases.
Edit:
Seems to be related to FLAG_ACTIVITY_NEW_TASK set in the Receiver, but.. then again I cannot start the app after boot is completed without this flag! Feels like a cycle I can't get out of..

Related

What causes "ActivityThread: Schedule relaunch activity"?

As the title says, I'm looking for the cause of ActivityThread: Schedule relaunch activity: foo.bar.MyActivity in my application log. I'm seeing this entry after MyActivity.onResume() but I'm not overriding that method and I'm not restarting the activity myself. I found this question related to the log entry but I'm not issuing any network requests or doing anything complicated in onCreate(). There are no configuration changes in the log, either, that might explain the activity being restarted.
I can't reproduce this on my devices and I only know of one instance of it happening so it seems to be something that happens very sporadically. In short, does anyone know why Android will sometimes restart my activity with the message in the title?
This might be happening if the app is in the background for awhile, Android kills off the OS process hosting the app, and then the user returns to the app (selects it from the list of recent tasks, or just clicks on the app icon on the HOME screen again). Android will then create a new OS process for the app and relaunch the Activity that was on the top of the Activity stack.

OnCreate is called multiply when app is started from Play Store App first and then from home screen

It occurred to me that under some circumstances our app seems to be restarted from scratch after being backgrounded. I managed to track to issue down to MainActivity.OnCreate being called multiply under the following circumstances
App is installed from APK and then ran from the installer, after backgrounding the app and starting it from the home screen it's reset to scratch
This behavior persists until the app is killed and then restarted from the home screen
App is run from Google Play app, after backgrounding the app and starting it from the home screen it's reset to scratch
If the app is backgrounded and then started from Play Store it's started correctly
App is run from home screen, after backgrounding the app and starting it from Google Play it's reset to scratch
When the app is foregrounded from the same launcher it has been started initially, OnCreate is not called again. There is at least one question reporting a similar behavior, unfortunately there is no answer providing a solution for the behavior.
When MainActivity.OnCreate is called, the instance of MainActivity seems to be a different instance than the initial one, since private members that are set in OnCreate are null when I'm trying to log them, anyway, the application context does not seem to be recreated from scratch, because AppCenter seems to be initialized right away on the second run, Xamarin.Forms starts up way quicker and static variables keep their values.
Is there any way to prevent this behavior and just keep a single instance of MainActivity active?
Congratulations! You've been bit by a long-standing, nasty Android bug which has been around since the dawn of time and is still broken, even though countless issues have been opened about it and the behaviour is reproducible and well-documented.
See the following:
https://issuetracker.google.com/issues/36907463
https://issuetracker.google.com/issues/36941942
https://issuetracker.google.com/issues/64108432
Re-launch of Activity on Home button, but...only the first time
App restarts rather than resumes
In September 2019, one of these issues was marked "fixed" with this comment:
Thanks for reporting this issue. The issue has been fixed and it will
become available in a future Android release.
So hopefully we will no longer be seeing this in Android Z ;-)
There is a workaround documented in my answer to
Re-launch of Activity on Home button, but...only the first time
in your android manifest set on the activity tag
android:launchMode="singleTop"
It will have consequences on how you handle notifications, and in some cases onActivityResult

UI not working properly after system killed app

My app uses a pagerTabStrip with fragments. When the app goes to background and other memory intensive tasks run in foreground, the app will be killed. Then, if I recall the app, the fragments buttons onclick methods dont fire anymore. (side note: the color of the buttons is then wrong too, but the "visual click" down->up is working)
It is working fine when I manually kill the app or when it's just short term via onPause/onResume in background. It seems that in this system-kill-case sort of image of the app is taken and reloaded (incorrect).
If there is no direct way to fix this, I would like to catch this event, to just recreate the whole fragment, but I can't. I tried onTrimMemory and investigated the call order of onStop/onDestroy/onRestart ... but I couldnt figure out, when this "special kill" happens.
Thank you!
Got it.
First, to reproduce the issue, just launch the app, send to background and then hit the red circle-cross at the left pane of the logcat output frame in android studio as described here:
https://medium.com/inloopx/android-process-kill-and-the-big-implications-for-your-app-1ecbed4921cb
My workaround is to save the processID in onSaveInstanceState and to check if this changed in onCreate as described here:
https://stackoverflow.com/a/47483856/6053524
If so, the app has been killed by system and my issues of not proper working UI occured. To fix this I load the initial Activity via new Intent/startActivity to get a clean new application.

Android App Launcher strange behaviour

hi guys i am finding the strange behaviour in android while launching the Application. Let me explain the senerio. I am launching my application from android's launcher page and my application starts and runs fine and after few minute i press home button and go to android home page and then go to launcher page and again select my application and it is starting it again from first but it should have resumed from the last place where i left. And when i press back button on the launch screen of second instance of my app i am able to go back to the last page where i have left. I am more confused about what was happening and it too happens sometimes only not every time. Hope you people could help me sort this problem, Hoping for better responses. Thanks in Advance.
Edit #1:
It is not happening in all the device it happens only with Samsung and Sony but works fine with LG and HTC.
To keep an activity running in the background is not in your hand. When you press the home button, your current activity goes to the background and can be killed (onDestroy() will be called) at any time depending on the need for memory of the other applications you launch.
The more apps you launch, the more chances of killing your background app is.
The behavior may be device specific - try saving your game settings in a persisted location within the 'onPause()' function, and retrieving it on 'onResume()'. Then it doesn't matter if a new activity gets launched or the old one gets called.

Is it fine if I see my application from Settings App in the emulator has Force Close option enabled even tough I have come out of my application?

I have written a simple database program in android. It runs fine, there is no force close error. But I checked from my application from Settings App I see the Force Close option enabled, which implies that my application is still running in the background, even though I have completely came out from my application to the home screen by pressing back key. And moreover I am not using any services, alarm or broadcast things.
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Your application is alive until Android OS needs more memory and destroys it. What I have understood does Android start destroying activities before killing the whole application. This means that your application can be alive even if you have finished your activities.
Do not worry about this; Android OS is handling this extremely well.

Categories

Resources