0

I want to clear some session data when user directly close the app means if app goes to background I want to clear data, how I recognize that app goes to background, I tried many solutions but they did not work for me,

 private boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }

    return isInBackground;
}

I'm using this method to find the status and I also tried another process which also didn't work for me.

I use Application.ActivityLifecycleCallbacks this process also I'm unable to find and I called this in Application class like this.

public class MyApplicationClass extends Application {


private static final String TAG = MyApplicationClass.class.getSimpleName();

private static MyApplicationClass sInstance;

@Nullable
public static Context getAppContext() {
    return sInstance;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"onCreate",Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate() called");
    sInstance = this;
    registerActivityLifecycleCallbacks(new ActivityCallbackApplication());
}
Community
  • 1
  • 1
  • Give us more information on data that you are planning to clear, what is that data exactly? – Akshay Katariya Feb 14 '18 at 12:11
  • Actually some data store in sq lite on client needs i have to clear. –  Feb 14 '18 at 12:12
  • write a function to clear those data that you are setting and call it when your app goes in the background according to your need whats the big deal? – Akshay Katariya Feb 14 '18 at 12:15
  • i created a method which clear the data but unable to find background state of app –  Feb 14 '18 at 12:17
  • 1
    you need clear data in `onDestroy` method. – Vikas singh Feb 14 '18 at 12:17
  • call your function in `OnDestroy` function – Milad Yarmohammadi Feb 14 '18 at 12:17
  • @vikassingh onPause is too early for clear data. – Milad Yarmohammadi Feb 14 '18 at 12:18
  • @milad if i clear data in onDestroy would it work, if i call onDestroy in mainactivity , when i switch activity then data should remain safe , it will clear when on background or app close. i have worked for app close. –  Feb 14 '18 at 12:19
  • not a good programming ethic though, but if you want to keep some data only during session, then in that case, dont save the data in sql but save it in the application class. It will be automatically removed once the application closes. – Mohit Feb 14 '18 at 12:25
  • You should create a base activity and extend all tour activities from it. so if user goes out from app you can clear data and this problem won't happen :) – Milad Yarmohammadi Feb 14 '18 at 12:25
  • Check this link..https://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo – Vikas singh Feb 14 '18 at 12:28
  • @milad okay when app goes to background and user get close the app from background, then onDestroy method will work for me, but when app goes to background and comes to app the it will be cleared, On thing work for me onDestroy. –  Feb 14 '18 at 12:29
  • @Mohit I know it is not good ethics and actually, i don't want to do it but my client's condition stuck at this point that's why can you tell me how can I find activity or app is launched from a background. –  Feb 14 '18 at 12:55
  • check this post... maybe this is what you need : https://stackoverflow.com/a/15573121/4336375 – Mohit Feb 15 '18 at 04:32
  • and this in case you want to check the google way. its written in kotlin. But i guess it is fairly easy to understand. https://stackoverflow.com/a/42679191/4336375 – Mohit Feb 15 '18 at 04:34

1 Answers1

0

Look at the new Lifecycle components by Google https://developer.android.com/topic/libraries/architecture/lifecycle.html

It allow you easily control lifecycle

public class MyApplicationClass extends Application implements LifecycleObserver {

    private static final String TAG = MyApplicationClass.class.getSimpleName();

    private static MyApplicationClass sInstance;

    @Nullable
    public static Context getAppContext() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"onCreate",Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate() called");
        sInstance = this;
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onBecameBackground() {
        // clear data here
    }

}

Deni Erdyneev
  • 1,779
  • 18
  • 26