How do I determine what app is currently in the foreground? - java

I need a way to log what app is currently running in the foreground, preferably without requiring root access or using the NDK. Is there any way to do this?

If I remember correctly, onResume() gets triggered any time an activity gets on foreground. If your app contains more than one activity, you should check all of them.
You can check activities lifecycle here

Related

Android Conditional PendingIntent in Notification?

My application currently has two activities: a splash activity which displays the app logo and loads the user’s data from a remote database, and then the main activity that houses all my fragments. I’ve reached a bit of an impasse right now with notifications. I’m using FCM to deliver the notifications and have that working fine, but don’t really know what to do with the pending intents of the notifications. If the app is closed, I don’t want it to launch the main activity since I load the data from the DB in the splash activity. Thus the main activity would have missing data (it currently crashes due to null pointer exceptions everywhere). But if the app is still running, I don’t want the splash screen to be launched again. Is there some type of way to have a conditional intent baked into the notification?
Alternatively, is this just bad practice to do what I’m doing with launching the activities from the notifications? I’m by no means an android expert so there might indeed be a better way. I would prefer to keep my current activity flow however where the splash screen is responsible for the initial loading of data. I was hoping there was some way to just broadcast a message on the click of the notification and if any activities have an active listener then it would just consume that data.
The solution I have come up with is to have a single activity whose sole purpose is to process notifications and then pass them to the appropriate activity either through a new intent (if app is terminated) or through a broadcast (if app is resumed). Not sure if this is bad practice (would appreciate anyone telling me if this violates any type of software principle).
It's working like a charm so far and is honestly really nice to have all that logic in one activity anyway - all the service has to do is pass in the data from the notification and the action and the activity does the rest.

Java - android - change activity of app while running in the background

I'm developing an Android app with several types of alarms and triggers.
One of these alarms trigger if you stop moving (GPS tracking) while it's active.
Now, when the app runs in the background when this triggers the client doesn't update when you switch back in, and the only notification received is a push-notification from the backend service. If I enter through the notification, the client loads the alarm correctly.
The code base is quite extensive, and due to time and resources it would be best to avoid huge refactoring tasks.
Is there an easy way to make the app go from Activity A to Activity B when it's running in the background?
You cannot change the current Activity in the background without bringing the app to the foreground (using startActivity()). However, you can surely tell the app that when it is brought to the foreground it should start a certain Activity or rearrange the back stack or whatever.
Post some of the code and maybe we can help more.

On destroy does not get call when app closes from recents - Android

I am trying to create a mock location app. After the user chose a location, the app should spoof his location to the place he chose.
I want that when the user closes the app, from recent apps or by pressing the back button the app will stop to mock location.
After reading the docs and some debugging, I understand that the OnDestroy function does not get called when the user stop the app from recent apps.
How can I know when the user decided to kill the app?
Yes, there is no guarantee that onDestroy method will be called everytime.onDestroy is called when Android goes short of resources and it need to clean up to recover some memory resources.
So, the best solution is to properly use onStop callback for deleting the location reference and initilize again in onStart.
For more details, check following links:
https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29
Activity OnDestroy never called?

How to detect is application been showing?

I have been developing Android application that use Activity with "download" button and Service for executing downloading in the background. And I have following task: to show message about downloading if application is currently displayed. How can I detect it? Is there standard Android OS functions for it? Thank you.
You can do this at the activity level, not for the application as a whole. Check out the description of the activity lifecycle. (Also see "Managing the Activity Lifecycle" in the Activities framework topic.) When your activity becomes visible, the framework will call it's onStart() method. When the user can interact with the activity, the framework will call the onResume() method. You can override one of these to know when your activity is showing or actually interacting with the user.
Note that, as described in the documentation, things work a little differently starting in Honeycomb (3.0).
The basic method is to use Broadcast receiver to share information between a service and an application. If your requirement is to update the status/progress of download on the main activity, you can use it.
Here is a simple tutorial about how to share information between a service and an activity. You can update the progress of download. It works for the activity that is currently running.
To check for it as the activity starts, you should override the onCreate, onStart() or onResume() functions.
Also there is a very neat solution presented in another question and i recommend that you should try to use that. It involves extending the Application class to store the information, and use its instance to check for updates. See this

Clearing Activity Stack

I am having trouble clearing my app's activity stack. At the start of my app I make the user login and give them a session id. After they login they are able to continue on using the app. However if there session expires I want to redirect them to the login activity and clear the Activity history so they can't have access to the app. I looked at the Android API and the Intent flag FLAG_ACTIVITY_CLEAR_TASK seems to be want I want but it was just included in API level 11 and no phones have the new OS yet. Does anyone have a solution to this problem. Thanks.
I found my answer here. Turns out that I have to broadcast an intent to tell all of the Activities to call the method finish().
The documentation for FLAG_ACTIVITY_CLEAR_TOP describes the situation you want if you use it in conjunction with FLAG_ACTIVITY_NEW_TASK
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
"This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager."

Categories

Resources