dany84
dany84

Reputation: 251

Android - How to know when app has been resumed after being in background

I'm trying to determine when my app is being resumed after the user closed it, in any way, pressing home button, back button or switching to another app.

What I need to do is to set a boolean when the app goes in background, so, when it is resumed, I know that it was in background before and I can act accordingly.

I tried to use onResume and onPause methods in activities to know when the app goes in background and it is then resumed, but as only one activity can be alive at at time, I had no success. When an activity is paused, this doesn't mean that the app went to background, because another activity could have been launched, but the onResume event of that activity will trigger only after the previous one has paused.

I've also tried to list all the apps in foreground, but with no success, if I put my app in background resuming another app, my app always results to be in the foreground.

I read that since Android 4 there is a new method to know when the app is in foreground, but I need my app to be compatible with Android 3.0 devices too.

Here is the code I tried putting in every single activity (MyApp is my Application name):

@Override
protected void onResume() {
    super.onResume();
    MyApp.isPaused = false;
}

@Override
protected void onPause() {
    super.onPause();
    MyApp.isPaused = true;
}

This is also my attempt to list all the apps in foreground:

ActivityManager activityManager = (ActivityManager)((Activity) currentContext).getSystemService( ACTIVITY_SERVICE );
  List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
  for(RunningAppProcessInfo appProcess : appProcesses){
      if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
          if(appProcess.processName.equals("com.xxx.myapp")) {
               Log.i("MyApp", "it's in foreground");
          }
          Log.i("MyApp", appProcess.processName);
      }
  }

Upvotes: 2

Views: 3067

Answers (6)

VoidExplorer
VoidExplorer

Reputation: 220

This class provides a singleton to determine "the activity in background" status. It uses a timer with a threshold(i.e. 0.3s) to determine the activity is went to background or not.

One thing has to point out is that if the user resumes to the activity within the threshold (i.e. 0.3s), this test will be failed.

If you have a better solution, please share with us :)

Ref: https://gist.github.com/steveliles/11116937

Upvotes: 2

MPG
MPG

Reputation: 795

public class CustomApplication extends Application {
    private static boolean activityVisible;
    @Override
    public void onCreate() {
        super.onCreate();

    }

    public static boolean isActivityVisible() {
        return activityVisible;
    }

    public static void activityResumed() {
        activityVisible = true;
    }

    public static void activityPaused() {
        activityVisible = false;
    }
}

and in your all activities set

@Override
    protected void onResume() {
        super.onResume();
        CustomApplication.activityResumed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        CustomApplication.activityPaused();
    }

and in your manifest

<application
        android:name=".CustomApplication"

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

You are absolutely correct :) Because only one activity can be alive at a time so you need something which remains alive through out the application life cycle :) like Application instance itself or you can also make use of shared preference for that matter. But seriously using shared prefference for checking lifecycle is wrong choice if you ask me.

If I was in your position I would have gone for Application class :) Here is code if you want to do the same :)

import android.app.Application;

/**
 * Created by sandeepbhandari on 3/3/16.
 */
public class AppService extends Application{
    private static AppService sInstance;
    public static boolean isGoingToBackGround=false;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
    }

    public static AppService getInstance() {
        return sInstance;
    }
}

In all your activities onPause just set

AppService service = AppService.getInstance();
        service.isGoingToBackGround =true;

And in onResume check the same variablethats all :) and yeah if you want to use your application class rather than default Application you have to make change to manifest.xml

<application
        android:name=".AppService"

Thats all :)

Upvotes: 2

Aelaf
Aelaf

Reputation: 118

i trust there is no need for u to post a code... that being said... start by logging every implemented methods onCreate(), onPause(), onDestroy(), and other well reputed Activity methods... but back button does not just pause it kills, thus onCreate is called most and check onStart() too.

Upvotes: 0

rmammadli
rmammadli

Reputation: 76

You can make Application class inside your project to save state of your project. When any activity goes to pause call on pause respectively while on resume call on resume method and save state of the inside this class. Even if one activity goes on pause another on resume your class will know exact state of the application. Or another way you can save applicaton state in shared preference in each activity can change its value.

Upvotes: 0

Flo We
Flo We

Reputation: 345

Override onTrimMemory(int level) in your Application. Might not be the prettiest way, but it has worked for me.

You will get

TRIM_MEMORY_BACKGROUND = 40;

when your application went into the Background.

Upvotes: 0

Related Questions