B4A Library [Class] Double Tap to Exit

Hy everybody,

This is my first class I have ever made. Maybe you know it from app like ES File Explorer: When you tap the backbutton a toastmessage appears with the text: 'Tap again to exit'. If you tap back again, the App will close. So i made this class to easy use this feature. (As I said earlier: this is my first class and i'm still working on it)

Example code:
B4X:
Sub Globals
Dim D As DoubleTaptoClose
End Sub

Sub Activity_Create(FirstTime As Boolean)
   D.Initialize ("Tap again to exit")
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If keycode = KeyCodes.KEYCODE_BACK Then
   D.TapToClose
   Return True
End If
End Sub


> Updates <

Version 1.1:
- Timeout set to ~1.8 seconds

Version 1.2:
- Remove the Log() method
- Changed the Description
 

Attachments

  • DoubleTaptoClose.zip
    473 bytes · Views: 640
  • DoubleTaptoClose 1.1.zip
    480 bytes · Views: 677
  • DoubleTaptoClose 1.2.bas
    811 bytes · Views: 796
Last edited:

yttrium

Active Member
Licensed User
Longtime User
Maybe add a timeout using a timer, so they have to double tap within, say, one second or else it doesn't trigger.
 

DonManfred

Expert
Licensed User
Longtime User
Really nice small but really useful class. Thank you for sharing!
 

tremara1

Active Member
Licensed User
Longtime User
Very useful I kept having to look up the process when I needed it, this is a real time saver for me...thanks.
 

wroyw

Member
Licensed User
I have change it a little bit and add an event before close :
B4X:
'Class module
Private Sub Class_Globals
   Private Ti As Timer
   Private I As Int
   Private TT As String
   Private BeforeCloseObj As Object
   Private BeforeCloseEvent As String
   Private CC As Int
End Sub

Public Sub Initialize (ToastText As String,CloseObject As Object , CloseEvent As String,ClickCount As Int,ClickTimeoutMs As Int)
   I = 0
   Ti.Initialize ("Ti",ClickTimeoutMs)
   Ti.Enabled = False
   TT = ToastText
   CC = ClickCount
   BeforeCloseObj = CloseObject
   BeforeCloseEvent = CloseEvent
End Sub

Private Sub Ti_Tick
  I = 0
End Sub

Public Sub TapToClose
   Ti.Enabled = False
   Ti.Enabled = True
   ToastMessageShow (TT,False)
   I = I + 1
   If I >= CC Then
     Ti.Enabled = False
     If (BeforeCloseObj <> Null) And (BeforeCloseEvent<>"") Then CallSub(BeforeCloseObj,BeforeCloseEvent)
     ExitApplication
   End If
End Sub

Example :
B4X:
Sub Globals
  Dim D As DoubleTaptoClose
End Sub

Sub Activity_Create(FirstTime As Boolean)
  D.Initialize ("Tap again to exit",Me,"BeforeClose",2,2000)
' Toastmessage = "Tap again to exit"
' Object for callback function = Me
' Event for callback function = "BeforeClose"
' Clickcount = 2
' Timeout for clicking = 2000 (ms)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
  If keycode = KeyCodes.KEYCODE_BACK Then
    D.TapToClose
    Return True
  EndIf
End Sub

Sub BeforeClose
  ' do something before close
End Sub
 
Last edited:
Top