Search code examples
androidonresumeunlock

cancel the events of lock/unlock on my android application


When i unlock my device to see my application, the process in onResume() is launched. How do I cancel the events of unlock, to avoid onResume() from processing?


Solution

  • To prevent unlock your device use below flag in your activity.

    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
    

    Code

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    

    Note :

    As stated on the Activity lifecycle docs, onCreate and onResume will always both be called the first time an Activity is started. When going back to Activity, at least onResume will be called