Ionic series--android dual exit app and physical back button hide keyboard implementation

Source: Internet
Author: User

1. Requirements Description

Because you want to control when you return to the previous page, if you have a keyboard, you need to hide the keyboard, so you need to customize the physical return key Click event of the Android phone, you define the priority of the return button, and then implement the first page and the login page to double-exit the operation of the app.

2. Preparation

Add Plugin $cordovatoast, $cordovaKeyboard

Cordova Plugin Add Https://github.com/driftyco/ionic-plugins-keyboard.gitcordova plugin add https://github.com/ Eddyverbruggen/toast-phonegap-plugin.git

3. Code implementation

The introduction of $cordovatoast, $cordovaKeyboard dependence.

Angular.module (' starter ',  [' Ionic ',  ' route ',  ' config ',  ' global ',  ' commonJs ',  ' Ngcordova '])     .run ([' $ionicPlatform ',  ' $rootScope ',  ' $location ',  ' $ Timeout ',  ' $ionicHistory ',  ' CommonJs ',  ' $cordovaToast ', ' $cordovaKeyboard ', function  ( $ionicPlatform,  $rootScope,  $location,  $timeout,  $ionicHistory, CommonJs, $cordovaToast, $ Cordovakeyboard)  {         $ionicPlatform. Ready (function  ()  {            // Hide the  accessory bar by default  (remove this to show the accessory  bar above the keyboard             if  (window.cordova && window.cordova.plugins &&  Window.cordova.plugins.Keyboard)  {                 Cordova.plugins.Keyboard.hideKeyboardAccessoryBar (True);             }            if  (window. StatusBar)  {                 // org.apache.cordova.statusbar required                 statusbar.stylelightcontent ();             }        });         //Physical return button control & double Exit app         $ Ionicplatform.registerbackbuttonaction (function  (e)  {             //to determine which page to double-exit             if  ($location. Path ()  ==  '/tab/todolist '  | |   $location. Path ()  ==  '/tab/donelist '  | |   $location. Path ()  ==  '/tab/account ' | |   $location. Path ()  ==  '/login ')  {                 if  ($rootScope. Backbuttonpressedoncetoexit)  {                      Ionic. Platform.exitapp ();                 } else {                      $rootScope .backbuttonpressedoncetoexit = true;                     $ Cordovatoast. Showshortbottom (' Press again to exit the system ');                     settimeout (function  ()  {                           $rootScope .backbuttonpressedoncetoexit = false;                     }, 2000);                 }             }else if  ($ionicHistory. Backview ())  {                 if  ($ Cordovakeyboard.isvisible ())  {                     $cordovaKeyboard. Close ();                 } else {                      $ionicHistory. GoBack ();                 }             }            else {                 $ rootscope.backbuttonpressedoncetoexit = true;                  $cordovaToast. Showshortbottom (' Press again to exit the system ');                 settimeout (function  ()  {                     $ rootscope.backbuttonpressedoncetoexit = false;                 }, 2000);             }            e.preventdefault ();             return false;         }, 101);     }]);


Ionic series--android dual exit app and physical back button hide keyboard implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.