How to tell when an Application (not activity) enters the background? - android

I have some utility code in my android application running as part of a shared component. It's scoped to the lifetime of the application, and launched as part of an overridden Application class.
What I'd like to know is if I can be notified when the application itself enters or leaves the foreground - basically the equivalent of iOS' applicationDidEnterBackground or foreground.
I've done a variety of searches, but everything comes back with people saying you should hook onPause and onResume on an activity - This would be great if my app only ever had one activity, however it has lots, and it doesn't seem sensible to hook onPause/Resume on every single one - let alone handling transitions between my activities

There isn't any direct approach to get the application status while in the background or foreground, but you can register your application class to the Activity Lifecycle Callbacks, just add your listener to the application like this:
myApplication.registerActivityLifecycleCallbacks(yourCallback);
and you will be able to know if you have activity on the foreground.

Related

How to detect when application is closed by a user in android?

I know that, unlike onCreate(), Application class does not have a onDestroy() method. But I wanted to know when my application is closed (or it is not visible on screen anymore). After all, whatsapp and many more similar chat applications can detect when user has left the app, and can record user's last online time. I want to achieve a similar thing. Also, when the application is destroyed, I want to detach all listeners attached to firebase databse.
I have already seen this question, but the accepted answer there is unreliable. So, what is the workaround for onDestroy() for me.
if you are talking about Application class (detecting when it is destroyed) - this is impossible, when Application gets killed developer shouldn't (and don't) have option for executing own code (as it may e.g. restart app from scratch)
but you are talking about app visibility, probably any Activity present on screen - extend Application class (and register it in manifest) and use ActivityLifecycleCallbacks with additional counting code: counter++ when any onActivityStarted and counter-- when onActivityStopped. also in onActivityStopped check if your counter==0, if yes then all your Activities are in background, so app isn't visible on screen (still it doesn't mean that its destroyed/killed)
edit: check out THIS example. or inspect supporting class ProcessLifecycleOwner (which probably is counting visible Activities for you and only calls onAppBackgrounded when all are gone)
You do not need onDestroy callback for it . You should be Doing it in onStop() of ProcessLifecycleOwner . Upon Application destroy your process will be destroys anyways in idle situation so no need to remove listeners there .
Remove the listeners in onStop and attach again in onStart . You can configure Application class with ProcessLifecycleOwner in a way so that Every Activity gets These callbacks. This is how it should works i guess if app is in background u will pop a notification of new message . Checkout ProcessLifecycleOwner.

Check if any activity is resumed for background service?

I've seen similar questions asked where they wanted to know if they could find if a specific activity was running from a service. This is not what I want to do. I want to be able to tell if the user is currently using any activity on my app.
My use case is that I have a monitoring Service that monitors many session-related things, such as a Socket connection. If the Socket connection fails at any point, the monitoring Service is made aware and will have to let the user know. However, I only want to let the user know if he is using my app. If he pressed on the home button to check something elsewhere, I don't want to let him know. If he is using my app, I show him a dialog.
My issue is that I need to be able to check if there is any Activity currently resumed. If so, then I show a dialog to my user.
I've seen many solutions that are in my opinion pretty bad. I don't want to hold a boolean that is constantly updated by activities to know if an activity is running. I want to be able to cleanly tell if there is a resumed activity and show a dialog if that's the case. Remember that I am in a Service, so I don't have a context.
You can use ProcessLifecycleOwner in the AndroidX Lifecycle library and check if the current lifecycle state is at least STARTED (visible to the user) or RESUMED (frontmost), depending on your preference. Its lifecycle is the composite of all Activity lifecycles in your app, so this will check if any Activity is in that state.
Also:
Remember that I am in a Service so I don't have a context.
Service extends Context, so you can use use the Service as a Context like you would with an Activity.

How to detect the completion of launching an app?

In Windows Phone, there is Application.LoadCompleted Event for detecting the finish of starting an application.
Note the finish means that users are able to interact with the app.
Is there similar API on Android? Any other approaches to achieve this?
after onResume() gets called it means the user can start interacting with your app. Consult this document for more information
You can override onCreate method of main Activity.
It depends what you mean by 'finished starting'. Activities, including the main activity, can be created and destroyed - and therefore onCreate() called - multiple times, for example (in the default behaviour) when the device orientation changes. onResume will be called even more often, for example every time the activity comes to the foreground. Then there's onStart(). See the Android developer docs to see when in the lifecycle each is invoked. It's true that the first time onResume() is called on the main activity will be when the app is fully started, but you won't know it's the first time without storing state somewhere outside any activity, for example on a singleton, or by subclassing the Application class, which is not something especially encouraged, as far as I can tell.

How to determine if application is in background or gets terminated?

I have an app that shall be extended with an app tracking code that should track the following events: app started, closed, foreground and background. My application has two activities.
For app started i use app.onCreate(). For foreground and background: onResume and onPause in each activity.
So my questsions are:
How do i detect when the application is completely gone? Something like onTerminate()?
When switching from the first activity to the second, i get a1.onPause() and then a2.onResume() and thus the events background followed by foreground. In that case i don't want those events. How do i know in onPause or onStop that i am staying in my app and only switching activities?
What i want is: App starts with a1 -> app started and app foreground event then switch to a2 -> nothing, press home -> background. When the app gets killed -> closed
At least i think i want that, or should one not use such a linear approach because of androids specific application and activity lifecycle and do something else?
How do i know in onPause or onStop that i am staying in my app and
only switching activities
I'm afraid you can't detect this only using callbacks.
How do i detect when the application is completely gone? Something
like onTerminate()?
AFAIK framework doesn't provide such callbacks for you.

How to manage application state in Android (for the iPhone developer)

I recently launched my first iPhone app and it seems to have people in the Android community asking for it ... so I started developing w/ the SDK.
The first thing I noticed is that in my iPhone app I would store certain session wide variables in the appDelegate. As I don't have this structure in Android I'm curious how Android developers keep track of application state across the app (hopefully w/out a ton of singleton objects?)
If the singleton object approach is how most developers do this - how can I ensure the application starts at a clean state each time the user clicks the "home" button and re-clicks icon (is there something I can add to my manifest to ensure it doesn't support multitasking in this way?)
My app has a lot of session specific state and for the first iteration won't yet support multitasking :(
First, android app can consist of multiple Activities.
If you want to share state between Activities use Application class: How to declare global variables in Android?
If you only have one Activity, then you can save state there.
Beware: when activity is not Active (it's GUI not showing) it does not mean that it is killed. So if you use your app and then close it, open another app, then go back to you app, it might be still "alive" (kept in memory) and the state would be preserved.
The best way to handle this is to hook into Activity lifecycle. Then you can set/reset data at will.
If you want to close the app when the user hits the "home" key, you can call finish() into your onPause method.
See this link:
Killing android application on pause
I find Singletons to be the best way to retain application state in Android applications. You can listen for when the user leaves the application through the onPause() and onStop() methods of the currently focused Activity, and do whatever you want with your data at that point. It's not good practice to try to override the OS's lifecycle management of your application (e.g. trying to kill your process when Back is pressed). If you want the app's state to reset every time the user leaves the application (via pressing Home, or being interrupted with a phone call or notification or what have you), simply put all your session data in the Activity itself. When the user leaves it will be destroyed and recreated when the user returns.
There are obviously specifics that I don't know about your application, but once you get familiar with the lifecycle of each screen (Activity) and of the application, you'll be able to use the callbacks to manage your state however you see fit.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Categories

Resources