Android Question App Lock - Detect that app was closed/minimized

Marcos Alves

Well-Known Member
Licensed User
Hello All,

how can I implement a feature like "app lock", with the behavior as follows:

- If any Activity of app is open, the user can navigate freely
- After all Activities are closed or minimized, when the user click on app icon an Activity with an edittext/password box or any unlock method is showed

Is it possible to detect, when closing or minimizing an Activity, that there is no one remaining open on the screen ? (to write a flag that could be checked at the start of each Activity signaling that the entire app was closed or in background?)
 

Erel

Administrator
Staff member
Licensed User
Create a class with:
B4X:
Public Sub ActivityPaused
 LastPausedTime = DateTime.Now 'LastPausedTime is a class global long variablt
End Sub

Public Sub ActivityResumed As Boolean
 If LastPausedTime + 5000 < DateTime.Now
  StartActivity(LockActivity)
  Return True
 End If
 Return False
End Sub
Initialize it in Service_Create of the Starter service.
The variable should be a public process global variable in the starter service.

Call in all Activities:
B4X:
Sub Activity_Resume
 Starter.Locker.ActivityPaused
End Sub

Sub Activity_Paused (UserClosed As Boolean)
 Dim Locked As Boolean = Starter.Localer.ActivityResumed
 'Maybe: If Locked Then Activity.Finished
End Sub
 

Marcos Alves

Well-Known Member
Licensed User
Create a class with:
B4X:
Public Sub ActivityPaused
 LastPausedTime = DateTime.Now 'LastPausedTime is a class global long variablt
End Sub

Public Sub ActivityResumed As Boolean
 If LastPausedTime + 5000 < DateTime.Now
  StartActivity(LockActivity)
  Return True
 End If
 Return False
End Sub
Initialize it in Service_Create of the Starter service.
The variable should be a public process global variable in the starter service.

Call in all Activities:
B4X:
Sub Activity_Resume
 Starter.Locker.ActivityPaused
End Sub

Sub Activity_Paused (UserClosed As Boolean)
 Dim Locked As Boolean = Starter.Localer.ActivityResumed
 'Maybe: If Locked Then Activity.Finished
End Sub
Thanks @Erel ,
Some considerations that I want to clarify in this code:
1. Shouldn't be (?):
B4X:
Sub Activity_Resume
   Dim Locked As Boolean =
   Starter.Localer.ActivityResumed
   'Maybe: If Locked Then Activity.Finished
End Sub

Sub Activity_Paused (UserClosed As Boolean)
  Starter.Locker.ActivityPaused
End Sub
2. You recommended to store LastPausedTime in a proccess global variable. But If Android destroys the app process LastPausedTime will be lost also and so I'm not sure that LastPausedTime + 5000 will be < than DateTime.now (null + 5000, maybe?). If yes, ok, but couldn't be more elegant and safe to store in app data using KeyValueStore or sql directly?

3. You told that the routine MUST to be added to each Activity_Pause and Activity_Resume which means that there is no an unique entry point when the app is fully minimized and there is no other way to know , from any Activity, that there wasn't other Activity open when a new Activity is called right?
 
Last edited:

Marcos Alves

Well-Known Member
Licensed User
1. You are correct.

2. Two options here: you can use KVS or just let it start with the lock screen when the process is created (it will happen automatically as LastPausedTime will be 0).

3. That's correct. You can see it by pressing in the home button and then go back to the app from the recent apps list.
-> LastPausedTime will be 0 (not null - ok !!!) - 0 is good:) . Null isn't - null is always a problem right? (even in sql queries...) - Thanks @Erel , your support is always valuable and a great differential for our community.
 
Top