The ionic Framework processes the Android return key. ionicandroid

Source: Internet
Author: User

The ionic Framework processes the Android return key. ionicandroid

In HybridApp mobile cross-platform development, the android platform will encounter some special and difficult-to-solve problems. These problems are very easy in native application development, and the Android return key processing is one of the problems, if we want to implement a function that prompts the user to exit the application by pressing the return key on the home page in many apps, it is very easy in native application development, you only need to process the return key event in the onKeyUp event. The Java code for exiting the application by pressing the return key twice is as follows:

Private long exitTime = 0; @ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & event. getAction () = KeyEvent. ACTION_DOWN) {if (System. currentTimeMillis ()-exitTime)> 2000) {Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); exitTime = System. currentTimeMillis ();} else {finish (); System. exit (0);} return true;} return super. onKeyDown (keyCode, event );}

However, in the HTML5 application using PhoneGap, things are not that simple. First, WebView takes over the event of the Return key, and you cannot process the return key at the Java layer, unless you change the code of the Cordova framework, this is obviously not suitable, it will bring about maintenance problems, and it does not conform to the general development specifications. Even if we can modify the Cordova source code and press the return key Webview to roll back the previous page and the processing prompt is displayed on the homepage, it is very difficult.

After in-depth analysis of the source code of the ionic framework and communication with developers outside the ionic forum, we finally found a better solution. As a mobile development framework of HybridApp, Ionic provides a reasonable solution for processing the return key of the Android platform. The ionic Framework processes the android return key source code as follows:

The priority definition of the Return key. It is mainly used to define the priority of the Return key action. For example, if there is a pop-up box (Priority 400), press the return key to cancel the pop-up box and do not roll back the page (Priority 100)

var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;var PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU = 150;var PLATFORM_BACK_BUTTON_PRIORITY_MODAL = 200;var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 400;var PLATFORM_BACK_BUTTON_PRIORITY_LOADING = 500;


The registration of the Return key processing action must be implemented here. Note that a function is returned. Calling this function will cancel the event registration.
        /**         * @ngdoc method         * @name $ionicPlatform#registerBackButtonAction         * @description         * Register a hardware back button action. Only one action will execute         * when the back button is clicked, so this method decides which of         * the registered back button actions has the highest priority.         *         * For example, if an actionsheet is showing, the back button should         * close the actionsheet, but it should not also go back a page view         * or close a modal which may be open.         *         * @param {function} callback Called when the back button is pressed,         * if this listener is the highest priority.         * @param {number} priority Only the highest priority will execute.         * @param {*=} actionId The id to assign this action. Default: a         * random unique id.         * @returns {function} A function that, when called, will deregister         * this backButtonAction.         */        $backButtonActions: {},        registerBackButtonAction: function(fn, priority, actionId) {          if(!self._hasBackButtonHandler) {            // add a back button listener if one hasn't been setup yet            self.$backButtonActions = {};            self.onHardwareBackButton(self.hardwareBackButtonClick);            self._hasBackButtonHandler = true;          }          var action = {            id: (actionId ? actionId : ionic.Utils.nextUid()),            priority: (priority ? priority : 0),            fn: fn          };          self.$backButtonActions[action.id] = action;          // return a function to de-register this back button action          return function() {            delete self.$backButtonActions[action.id];          };        },


Go back to the question we just raised, and press the return key on the home page to exit the application. On other pages, we can normally return to the previous page, as long as we register a processing event. The Code is as follows:

. Run (['$ ionicPlatform', '$ ionicPopup', '$ rootScope', '$ location', function ($ ionicPlatform, $ ionicPopup, $ rootScope, $ location) {// The exit prompt box is displayed on the homepage $ ionicPlatform. registerBackButtonAction (function (e) {e. preventDefault (); function showConfirm () {var confirmPopup = $ ionicPopup. confirm ({title: '<strong> exit application? </Strong> ', template:' Are you sure you want to exit the application? ', OkText: 'stop', cancelText: 'cancel'}); confirmPopup. then (function (res) {if (res) {ionic. platform. exitApp ();} else {// Don't close});} // Is there a page to go back? If ($ location. path () = '/home') {showConfirm ();} else if ($ rootScope. $ viewHistory. backView) {console. log ('currentview: ', $ rootScope. $ viewHistory. currentView); // Go back in history $ rootScope. $ viewHistory. backView. go ();} else {// This is the last page: Show confirmation popup showConfirm ();} return false ;}, 101) ;}])




Android development framework

Google's gson package can be used for json parsing.
The Network request and cache mechanism (generally referred to as image cache) can be viewed by volley.
The database is relatively simple. The android app is already encapsulated. But you can check FinalDb in Afinal.
The UI is useless. It is written by yourself. You can master the view and viewgroup. Basically, you can master all the controls.

To learn about android development, we also need to learn more about the three major SSH frameworks?

No, it doesn't matter ~

Android has its own framework components. All you need to learn is its four major components and the design mode in program development.

The design mode is useful wherever it is ~

The three SSH frameworks of J2EE have no shadows in android ~

Related Article

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.