How can I tell if my Application is resumed from background?

前端 未结 5 2040
北恋
北恋 2021-02-04 10:57

I want to lock my application when it goes in background and when it resumes I want to display my own lock screen. The lock screen is an Activity of my application.

Aft

5条回答
  •  天命终不由人
    2021-02-04 11:32

    You can achieve that if you have a global Activity "MyActivity" and all the activities extend from it.

    Then you override onPause and onStop methods on "MyActivity"

    @Override
    public void onPause()
    {
        super.onPause();
        setLockStatus(false);
    }
    
    @Override
    public void onStop()
    {
        super.onStop();
        setLockStatus(true);
    }
    

    and:

    @Override
    public void onResume()
    {
        super.onResume();
        checkLockScreen();
    }
    

    EDIT: Obviously you need to create the methods setLockStatus and checkLockScreen and do whatever you want (like save the status on sharedPreferences).

提交回复
热议问题