Ionic Framework for Android return key processing

Source: Internet
Author: User

In the Hybridapp Mobile cross-platform development, the Android platform will encounter some special and difficult to solve problems, these problems in the native application development is very easy, Android's return key processing is one of the problems, If we're going to implement a feature in many apps that prompts the user to exit the app on the home page by pressing the Back button pop-up dialog box, it's easy to do this in native app development, as long as the onkeyup event handles the return key event. The Java code to exit the app by pressing the 2-time return key is as follows:

[Java]View Plaincopy
  1. Private Long exittime = 0;
  2. @Override
  3. Public boolean onKeyDown (int keycode, keyevent event) {
  4. if (keycode = = Keyevent.keycode_back && event.getaction () = = Keyevent.action_down) {
  5. if ((System.currenttimemillis ()-exittime) > ) {
  6. Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show ();
  7. Exittime = System.currenttimemillis ();
  8. } Else {
  9. Finish ();
  10. System.exit (0);
  11. }
  12. return true;
  13. }
  14. return Super.onkeydown (KeyCode, event);
  15. }


But in the HTML5 application that uses phonegap, things are not so simple, first webview take over the event of the return key, you can't process the return key in the Java layer, unless you change the code of the Cordova framework, but this is obviously inappropriate, it will bring maintenance problems, Also does not conform to the general development specifications. Even if we can modify the Cordova source code, at the same time processing good press the return key WebView back to the previous page and on the homepage when the popup processing hints, is also very difficult.

In-depth analysis of the Ionic framework source code, in the Ionic Forum with foreign developers exchange, finally found a comparison of the solution. Ionic as a more active foreign Hybridapp mobile development Framework, the processing of the return key of the Android platform is a reasonable solution. Ionic Framework for Android return key processing source code is as follows:

Returns the key priority definition, the primary purpose of which is the priority definition of the return key behavior, for example, when there is a popup (priority 400), press the back key to cancel the popup box, do not fallback page (priority 100)

[JavaScript]View Plaincopy
    1. var platform_back_button_priority_view = 100;
    2. var platform_back_button_priority_side_menu = 150;
    3. var platform_back_button_priority_modal = 200;
    4. var platform_back_button_priority_action_sheet = 300;
    5. var platform_back_button_priority_popup = 400;
    6. var platform_back_button_priority_loading = 500;


Register the Return key processing action, our own processing of the return key needs to be implemented here, note that the return is a function, call this function will cancel the event registration.

[JavaScript]View Plaincopy
  1. /**
  2. * @ngdoc method
  3. * @name $ionicPlatform #registerbackbuttonaction
  4. * @description
  5. * Register A hardware Back button action. Only one action would execute
  6. * When the Back button was clicked, so this method decides which of
  7. * The registered Back button actions have the highest priority.
  8. *
  9. * For example, if a actionsheet is showing, the back button should
  10. * Close the Actionsheet, but it should isn't also go back a page view
  11. * or close a modal which may be open.
  12. *
  13. * @param {Function} callback called when the Back button is pressed,
  14. * If this listener are the highest priority.
  15. * @param {Number} Priority only is the highest priority would execute.
  16. * @param {*=} ActionId the ID to assign this action. Default:a
  17. * Random Unique ID.
  18. * @returns {function} A function that, when called, would deregister
  19. * This backbuttonaction.
  20. */
  21. $backButtonActions: {},
  22. Registerbackbuttonaction: function (FN, priority, ActionId) {
  23. if (!self._hasbackbuttonhandler) {
  24. //Add a Back button listener if one hasn ' t been setup yet
  25. Self. $backButtonActions = {};
  26. Self.onhardwarebackbutton (Self.hardwarebackbuttonclick);
  27. Self._hasbackbuttonhandler = true;
  28. }
  29. var action = {
  30. ID: (ActionId actionid:ionic. Utils.nextuid ()),
  31. Priority: (priority:0),
  32. Fn:fn
  33. };
  34. Self. $backButtonActions [action.id] = action;
  35. //Return a function to de-register this back button action
  36. return function () {
  37. Delete self. $backButtonActions [action.id];
  38. };
  39. },


Back to the question we just started, on the homepage add press the Back button to exit the application, on the other page normal return to the previous page, as long as the registration of a processing event can be, the code is as follows:

[JavaScript]View Plaincopy
  1. . Run ([' $ionicPlatform ', ' $ionicPopup ',' $rootScope ', '$location ', function ($ionicPlatform, $ Ionicpopup, $rootScope, $location) {
  2. //Main Page display exit prompt box
  3. $ionicPlatform. Registerbackbuttonaction (function (e) {
  4. E.preventdefault ();
  5. function showconfirm () {
  6. var confirmpopup = $ionicPopup. Confirm ({
  7. Title: ' <strong> exit Application?</strong> ',
  8. Template: ' Are you sure you want to quit the app? ',
  9. Oktext: ' exit ',
  10. Canceltext: ' Cancel '
  11. });
  12. Confirmpopup.then (function (res) {
  13. if (res) {
  14. Ionic. Platform.exitapp ();
  15. } Else {
  16. //Don ' t close
  17. }
  18. });
  19. }
  20. //Is there a page to go?
  21. if ($location. path () = = ' home ') {
  22. Showconfirm ();
  23. } Else if ($rootScope. $viewHistory. Backview) {
  24. Console.log (' CurrentView: ', $rootScope. $viewHistory. CurrentView);
  25. //Go back in the history
  26. $rootScope. $viewHistory. Backview.go ();
  27. } Else {
  28. //This was the last page:show confirmation popup
  29. Showconfirm ();
  30. }
  31. return false;
  32. }, 101);
  33. }])

Ionic Framework for Android return key processing

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.