Android: Application onCreate called every time app goes to background - android

I've a strange behavior with an android app when it runs on a S3 with android 4.3: every time app goes to background (pressing back button), when it's resumed, it calls the onCreate() method of Application class.
Reading the Android documentation, Application onCreate() should be called the first time app runs, when app is destroyed or if the device has low memory, but seems it's not my case (I added a log into onLowMemory())
So my question is: in which conditions Application onCreate() method is called? Does it depends on device and/or android versions?

Related

Kill Application Xamarin Forms

What event can I get to when user kills the application by selecting the button to show the programs that are running on the mobile and dragging up or down.
I don't think there is an event that triggered when people select the button to show the programs that are running on the mobile and dragging up or down in Xamarin.forms.
In Xamarin.forms, the Application class contains three virtual methods that can be overridden to handle lifecycle methods:
OnStart - Called when the application starts.
OnSleep - Called each time the application goes to the background.
OnResume - Called when the application is resumed, after being sent to the background.
Note that there is no method for application termination. Under normal
circumstances (i.e. not a crash) application termination will happen
from the OnSleep state, without any additional notifications to your
code.
Refer: app-lifecycle

What causes this weird app life-cycle?

My Android app checks if a specific service is not running, in my Activity's onResume method. If it hasn't been started, then it goes into the background (via startService) and stays there for an action. Clicking on a button causes the service to go into the foreground and it works great. Now the problem:
Scenario #1 :
The foreground service has a button, which sends it back to the background and stops it. I click on it and after that I close my app (the activities from the recents view). Even the Android monitor shows me that it's not running and works as it should.
Scenario #2: (The not expected behavior)
First I close the app (the activities) and then I click on the service's close button. In this case, I can clearly see that my app still uses the same amount of memory it used while the activities were opened. When I reopen my app, I can see that it continues working where I left it (at least the internal app variables don't get garbage collected) Sometimes the CPU usage monitor shows that methods get called, but not from my app. Only Android SDK functions. I couldn't see objects kept in the memory, but a lot of chat[], String, FinalizerReferences etc...
What could cause this? Ruined context lifecycle? Memory leak?

Is there a method inside android to know when the system is going to kill my app?

We have one very difficult to reproduce problem with an app. The use case is as follows:
User opens app
User leaves app in background
User openes 5 to 7 more apps
System kills our app
When user tries to resume app, app crashes due to NullPointerException
I was trying to use console log with Application class method onTrimMemory() and onLowMemory() but this methods are not being called. Is there any method or callback I can be listening to know when android system will kill my app due to many more apps being opened and in that case for me to do something?
You can save whatever states you need in onSaveInstanceState() and restore them in onRestoreInstanceState(). onDestroy() is what the system will most often call to clear room while your Application is in the background, but it is not guaranteed to be called, so it is better to have already saved your states in onSaveInstanceState().
Is there any method or callback I can be listening to know when android system will kill my app due to many more apps being opened and in that case for me to do something?
Not really. Depending on circumstances, onDestroy() of running activities and services will be called.
We have one very difficult to reproduce problem with an app
That should be reproducible in a matter of seconds:
Run your app.
Switch to another app.
Kill your app's process from your IDE (e.g., "Terminate Application" toolbar button in "Android Monitor" tool window).
Try returning to your app from the overview screen (a.k.a., recent-tasks list)

Problems keeping service alive through rotate, killing when leaving app, and turning screen off in landscape

I have a service that I need to meet the following conditions
1) The service needs to be running while the app is in the foreground.
2) When the activity rotates, do not stop the service
3) The service must remain active even if you minimize the app when one specific fragment is open.
4) When the activity is stopped (not via rotate) and it is not the specific fragment, stop the service.
I'm starting/stopping my services in the onstart/onstop. I ended up using a boolean to track if the screen was rotating.
I'm starting/stopping my services like this
context.getApplicationContext().startService(intent)
context.getApplicationContext().stopService(intent)
It worked perfectly except for one situation.
When you turn the screen off in landscape, Android for some reason decides it needs to recreate the whole activity, and calls through the entire life cycle. I've reproduced this in a basic app on 4.4 and 5.0.1, and it's been shown in other threads such as this.
Problems understanding the life cycle when screen goes off and on
When it does this, the rotating/not rotating can't be relied on, and I'm guessing due to how quickly things were happening (multiple onStop/onStarts), it was causing race conditions. When I would return to the app, sometimes it worked, sometimes it would hang (no errors), sometimes two services would be running.
It became a rabbit hole where I started doing checks like, was the screen off when it came into the onStart, so the next time onStart was called it knew it was coming back in from an screen off recreation that was being flagged as rotated (since Android was now rotating it back into landscape from the portrait lock screen). It was getting ridiculous, and not working well.
One thread suggested using a headless fragment for it to survive rotation, but I still need it to stop when the app is closed, so I end up getting stuck with the same problem.
Are there any recommended ways to go about solving this? The service can't be allowed to remain active if the app is closed (except for the 1 fragment), it'll just be a huge battery drain, and it needs to survive rotations or it will interrupt things.
Why use a service if you don't need it to be running when the app is closed? Just fire off a background thread when the app starts and do your work there.
UPDATE:
Instead of starting your service from an Activity, you could start it from your Application class (or a descendant thereof). You could stop it when your app is backgrounded or killed and then start it again if the app is resumed.
The most common way to do this is to keep track of how many Activities are currently in the resumed state. This means in every Activitie's onPause() and onResume() you decrement or increment this counter. If the counter is at 0, you know you app is in the background. Note: you probably want to add a delay to decrementing the counter in onPause() since there will of course be times when you transition from one Activity to another (or switch orientation) where Activity A will be paused before Activity B starts.

Any way to force app out of memory in Android emulator?

I've implemented application state saving/loading in onSave/RestoreInstanceState and onCreate in one of my android app's activities. Is there any way to force the emulator to remove my app from memory so that onRestoreInstanceState is called?
Currently it looks like my app just stays in memory (for a time longer than I am willing to wait anyway). When I hit the "home" button when my activity is active I get the following method calls:
onSaveInstanceState
onStop
When I reactivate my app, all I get is
onRestart
Is there any way in the emulator to force my app to be mothballed so that onRestoreInstanceState and/or onCreate are called again?
Use the DevTools app ("Immediately destroy activities").

Categories

Resources