简体   繁体   中英

Clearing the data when my Application goes to background

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());
}

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM