B4A Library [Class] Double Tap to Exit

Discussion in 'Additional libraries, classes and official updates' started by Penguin, May 21, 2013.

  1. Penguin

    Penguin Member Licensed User

    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:
    Code:
    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
     

    Attached Files:

    Last edited: Sep 6, 2015
  2. gadgetmonster

    gadgetmonster Active Member Licensed User

    Thanks Penguin - just what I was looking for :)
     
  3. yttrium

    yttrium Active Member Licensed User

    Maybe add a timeout using a timer, so they have to double tap within, say, one second or else it doesn't trigger.
     
  4. DonManfred

    DonManfred Expert Licensed User

    Really nice small but really useful class. Thank you for sharing!
     
  5. tremara1

    tremara1 Active Member Licensed User

    Very useful I kept having to look up the process when I needed it, this is a real time saver for me...thanks.
     
  6. wroyw

    wroyw Member Licensed User

    I have change it a little bit and add an event before close :
    Code:
    '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 <> NullAnd (BeforeCloseEvent<>""Then CallSub(BeforeCloseObj,BeforeCloseEvent)
         
    ExitApplication
       
    End If
    End Sub
    Example :
    Code:
    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: Mar 24, 2017
    rraswisak likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice