JoeyG
JoeyG

Reputation: 610

How can I tell difference between app in background and app closed with Android?

I am trying to find a way to tell if an application has been closed or has went to the background. I need the ability to do this with any application. I am using the AcitivityManager to poll which applications are running, and compare them to an application I am looking for. The problem is that the importance level gets set to IMPORTANCE_BACKGROUND when it is actually in the background AND when I use the back button to "close" the app. How can I tell if it is actually closed or in the background?

private boolean isActivityRunning(){
    ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++){
        Log.d(TAG, "Polling Serivce at application " +procInfos.get(i).processName);
        if(procInfos.get(i).processName.equals(appLaunchedPackage)&&
            (procInfos.get(i).importance ==RunningAppProcessInfo.IMPORTANCE_VISIBLE || 
                    procInfos.get(i).importance ==RunningAppProcessInfo.IMPORTANCE_FOREGROUND || 
                    procInfos.get(i).importance ==RunningAppProcessInfo.IMPORTANCE_BACKGROUND)  ) {
            Log.d(TAG, "Polling Serivce - the app has been found!");
            return true;
        }
    }

Upvotes: 0

Views: 1131

Answers (2)

Shlublu
Shlublu

Reputation: 11027

When you use the back button to switch back from your app to the desktop, the application goes background. It is not closed.

Android will close it later, if needed, and this will not be the result of a user action.

Upvotes: 0

Peter Knego
Peter Knego

Reputation: 80340

App in android in not "closed" when you hit back button. Back button invokes previous Activity on activity stack and puts current one in the background.

So home button clears the activity stack and invokes launcher app, back button invokes previous activity from stack. Both put current app in background.

Apps on android are only closed, i.e. killed and removed from memory, when system is low on resources.

Upvotes: 1

Related Questions