In Android when you hold down the home button you can bring up a task list of your recent apps. And each "card" in the task list should have a preview of the app. However, this time I opened the task list and the previews were blank (except for the app title).
I had the phone Settings in the task list and every time I would tap on the task and try to open it in would close immediately.
I also had this app I was developing and when I tapped on the task to open the app my RecyclerView wasn't showing anything, when it should be.
I also was playing AngryBirds 2, so was it that Android had silently killed those apps for memory? If so, does onCreate() of the killed apps get called again when this happens?
Edit: I just noticed if you close an app too quickly when it's transitioning to an Activity you can get a blank preview.
Edit 2: So to answer my question, after referencing the Activity lifecycle diagram this is what I found. When another application needs memory, the app's process will be killed and when the user navigates back to the app, onCreate() will be called again. However, that doesn't explain why I couldn't reopen the Settings. And I have no idea how to reproduce this weird behavior!
Ok after some debugging, I found out that Android did indeed kill my app because it needed memory. When you resume to your app however, onCreate will be called with a savedInstanceState that is NOT null. I had some logic that required savedInstanceState to be null and therefore my RecyclerView wasn't populated.
To recreate this scenario of Android killing my app I had to open a ton of apps lol. Is there way I can reproduce Android killing my app without opening all those apps? Hm...
Related
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.
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
I have multiple activities in my app and a specific problem in which after my app is killed by the system, upon reopening the app (icon or from the overview) it starts loading my launcher activity with the splash screen instead of loading the last activity that the app was using.
My launcher activity is essentially a form of a login screen which should be used only the first time the app is opened, but instead it is reopened every time the app is killed by the system and app reopened by the user.
I am inexperienced and do not know if this is the default behavior of Android or a bug, I have searched through stack overflow but could not find a conclusive answer.
Coincidentally this well known long standing Android bug has appeared. (https://issuetracker.google.com/issues/36941942)
I do not know if the two problems might coincide, and I cannot be sure when the longstanding bug has first appeared because previously I was testing through Instant Run which does not result in the bug, but am now forced to test through play because it does result in the bug.
Thanks.
I am inexperienced and do not know if this is the default behavior of Android or a bug
This is the default behavior of Android, if there is no current task associated with your app. That includes:
User swiping the task off of the overview screen
You terminating the process and task from inside Android Studio
All the time if the user has been out of the app for 30+ minutes
User using Force Stop on your app in Settings
And probably some other scenarios that I'm not thinking of right now
My launcher activity is essentially a form of a login screen which should be used only the first time the app is opened
I recommend that your launcher <intent-filter> be on your primary activity in the app. If that activity determines that the user has not logged in, it can do something to authenticate the user (display a fragment, start an activity, etc.).
The Android docs say
If the user continues to press Back, then each activity in the stack is popped off to reveal the previous one, until the user returns to the Home screen (or to whichever activity was running when the task began). When all activities are removed from the stack, the task no longer exists.
Well I tried this on a galaxy s4 with android 4.3, but afterwards when I hold the home button to show all tasks the task is still there (tried with settings app, help app, internet app, my own app). In what way does it not exist?
Edit:
I'm reading up on tasks to understand the Android system. I have no specific app development problem.
I am basically looking for the same functionality found in the DevTools (Development.apk) app that comes with the emulator. I am wanting to perform similar testing on an actual device but the DevTools app does not work properly on the device I have so I cannot use it.
I am looking to test in a similar way.
What this does is causes each Activity to be destroyed whenever it leaves the screen, holding onto its instance state just as if the system needed resources and had killed it. (So I can't just call finish)
Is there a way to do this?
Thanks
You can kill your app's process at any time using the DDMS stop button. highlight your application in the list and click the stop sign button. your application will be destroyed like it was killed by the system.
See the stop button in the left pane(Devices) above each device listing: