Multitasking event handlers in Android as in iOS - android

In iOS when an Application goes to background or when it becomes active, event handlers in App delegate gets invoked. Is there any similar event handlers for Android's Multitasking?

Yes, see the activity LifeCycle here:
http://developer.android.com/reference/android/app/Activity.html

Not that I know of. I needed the same type of notification in my app as well to do some goes to background cleanup and comes to foreground restore type operations. What I did was subclass Activity and have that subclass call to a manager I wrote to track if the app was in foreground or background. In that subclassed activity call the manager to notify on activity "visible lifetime" events. I.E. call to say in foreground in the activites onStart and call to say in background in onStop. See this link for details of "visible lifetime" scroll down about 2 pages.
I then used my new subclassed activity as the bases for every activity in my program. So now your manager will be able to know whenever a foreground state change occurs. I then used the observer pattern in the manager to allow other objects to register to be notified of foreground state changes. This effectively mimics what we are used to coming from iOS. There may be better ways to do this but this works just fine.

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.

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

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.

Remove notification on fragment's onStop()

So I have an ongoing notification that I only want to remove when the application is closed. I removed the notification on OnDestroy when I used it in an activity. Now I want to use it in a fragment, but since the onDestroy method is not always called I remove it on onStop. When I press the home button, the notification disappears. How can I do this? (I also tried onDestroyView and that doesn't work either.)
application is closed
there's no concept of closing application really on Android.
but since the onDestroy method is not always called
That's actually fine. Putting app in background is not equivalent of killing it. There may be no need from system perspective to get your code killed/garbage collected.
Also removing notifications when user puts your app in background pretty much indicates these notifications are useless from user perspective.
onDestroy method is not always called I remove it on onStop
It indicates your notifications are tightly coupled with Fragments. That's sounds bad idea. I'd rather delegate notification handling out of the Fragment and then manage its visibility (if that really makes any sense to do) in code triggered i.e. via code utilizing ActivityLifecycleCallbacks or recent Lifecycle-aware Component.

Event on launch/stop of visible part of application

Stackoveflow!
I have an application with sticky background service and visible part, consisted of several activities.
I need to track start and stop of visible part (all activities).
For example:
When user starts MainActivity, Service receives LocalBroadcast that tells it to start something.
When user rotates screen, Service must not receive anything.
When user goes to SecondActivity, Service again must not receive anything.
When user closes ALL ACTIVITIES, Service gets LocalBroadcast telling it to stop something.
If I use Activity. onCreate and onDestroy, or onStart and onStop, I get events related to lifecycle of single activity. Also I get events related to screen rotation.
I also cannot use Application. onCreate or onTerminate, as they will not trigger because of service running in the background.
I need to track real start and stop of entire application except service.
See: http://developer.android.com/guide/components/bound-services.html
When the last client unbinds from the service, the system destroys
the service (unless the service was also started by startService()).
So each Activity should bind() to the service, in onCreate, and unbind() in onStop/onDestroy
Now to fix the problem with the orientation changes, you can check for isFinishing in the onStop/onDestory callback
(see: How to distinguish between orientation change and leaving application android)
and also put some flag in onSaveInstanceState callback, so that in the following onCreate you can check for the existence of that flag, and act accordingly (refresh the binding, or avoid calling bind() again)

Android : how to know when an app enters or the "background" mode?

I am trying to achieve the following with Android :
when the app is in background, a thread polls a server every now and then to retrieve data and notifies the user if new data is available. I am using a Service for that, fine.
when the app is in "active" use, i.e. one of its activities is visible, the polling should stop as it might interfere with other user actions.
I don't understand how to detect the transition between the "active" or "background" use of the app. The onResume() activity methods does not seem to help, as an activity can be hidden or visible during "active" use anyway. My understanding is that the app itself doesn't make the difference between the 2 states.
Can it be related when the HOME button is pressed ? Is there another way to do the distinction ?
I am thinking of an equivalent of iPhone's app delegate method applicationDidEnterBackground. Is it the right way to think with Android ? Or shall I use another approach ?
Thank you.
I'm going to reference the Activity Lifecycle. In between onResume and onPause your Activity is 'active', i.e., it's on the screen and the user can interact with it. If your activity's onPause method is called then you should assume that it is no longer 'active' and the user cannot interact with it anymore until onResume is called again. If you wish to track this in your service you're going to have to do this manually.
This is probably most easily achieved by calling a method in your service in Activity#onResume that increments a counter or sets a flag and in onPause reverting that change. If you have multiple activities then you're most likely going to need a counter, probably an AtomicInteger, and use it to determine when you should resume your polling.
I would probably wait for a small bit of time when the counter reaches 0, recheck it, and if it is still 0 resume polling. This would account for the gap between one activity's onPause and another's onResume.

Categories

Resources