Quante volte è capitato di vedere quel messaggio che ci dice di premere due volte per uscire dall'app?
Ora vediamo come creare questa funzione.
Per prima cosa dobbiamo creare e dichiarare una variabile all'interno della funzione onCreate:
Poi bisogna creare una funzione:
Questo è il risultato finale
Ecco un video dimostrativo
Ora vediamo come creare questa funzione.
Per prima cosa dobbiamo creare e dichiarare una variabile all'interno della funzione onCreate:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dichiarazione della variabile doubleBackToExitPressedOnce e settata come FALSA | |
private var doubleBackToExitPressedOnce = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBackPressed(){ | |
//visualizzazione messaggio nella SnackBar che informa di premere il tasto due volte per uscire | |
snackBar = Snackbar.make(root_layout, "Premi due volte per uscire",Snackbar.LENGTH_SHORT ) | |
//se la variabile doubleBackToExitPressedOnce è VERA l'app viene chiusa | |
when { | |
doubleBackToExitPressedOnce -> { | |
super.onBackPressed() | |
return | |
} | |
} | |
//settaggio della variabile da FALSA a VERA | |
this.doubleBackToExitPressedOnce = true | |
//visualizzazione della SnackBar | |
snackBar!!.show() | |
//trascorso il tempo indicato in delayMillis la variabile | |
//doubleBackToExitPressedOnceviene settata come FALSA | |
Handler().postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 1000) | |
} |
Ecco un video dimostrativo
Download Project | ![]() |
Download file APK | ![]() |
Commenti
Posta un commento