Rajesh
Rajesh

Reputation: 133

How to check whether activity is in foreground or background in android?

I have a activity in which in onCreate method I have to check whether activity is in foreground or background .if activity is in foreground then I have to send request to to server and if app goes in background then I have to send that server request through Intent service.How can I do that.

Is there any way if app goes in background stop server request which is in foreground .

Upvotes: 3

Views: 7470

Answers (5)

TMtech
TMtech

Reputation: 1166

Here is a solution using Application class.

public class AppSingleton extends Application implements Application.ActivityLifecycleCallbacks {

private WeakReference<Context> foregroundActivity;


@Override
public void onActivityResumed(Activity activity) {
    foregroundActivity=new WeakReference<Context>(activity);
}

@Override
public void onActivityPaused(Activity activity) {
    String class_name_activity=activity.getClass().getCanonicalName();
    if (foregroundActivity != null && 
            foregroundActivity.get().getClass().getCanonicalName().equals(class_name_activity)) {
        foregroundActivity = null;
    }
}

//............................

public boolean isOnForeground(@NonNull Context activity_cntxt) {
    return isOnForeground(activity_cntxt.getClass().getCanonicalName());
}

public boolean isOnForeground(@NonNull String activity_canonical_name) {
    if (foregroundActivity != null && foregroundActivity.get() != null) {
        return foregroundActivity.get().getClass().getCanonicalName().equals(activity_canonical_name);
    }
    return false;
}
}

If you have a reference to the required Activity or using the canonical name of the Activity, you can find out whether it's in the foreground or not. This solution may not be foolproof. Therefore your comments are really welcome.

Upvotes: 1

Mykhailo Yuzheka
Mykhailo Yuzheka

Reputation: 753

Check this library. It can ctrack current activity status.

Upvotes: -1

Tejas Parmar
Tejas Parmar

Reputation: 79

The best approach would be you should trigger your service in onStop() call of your activity, as the service gets start your logic in onStatCommand() will be execute and if you want your service should not be dependent on app lifecycle return START_STICKY with in onStatCommand().

Upvotes: 1

Vivek_Neel
Vivek_Neel

Reputation: 1353

Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods.

Example Implement custom Application class :

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

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

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

  private static boolean activityVisible;
}

Register your application class in AndroidManifest.xml

and then so something like this :

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

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

Upvotes: 1

Abhishek Patel
Abhishek Patel

Reputation: 4328

Try This for check your app is in foreground or not

public static boolean isAppInForeground(Context ctx) {
    ActivityManager activityManager = (ActivityManager) ctx
            .getApplicationContext().getSystemService(
                    Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> services = activityManager
            .getRunningTasks(Integer.MAX_VALUE);

    if (services == null) {
        return false;
    }

    if (services.size() > 0
            && services.get(0).topActivity
                    .getPackageName()
                    .toString()
                    .equalsIgnoreCase(
                            ctx.getApplicationContext().getPackageName()
                                    .toString())) {
        return true;
    }

    return false;
}

Upvotes: 1

Related Questions