Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ionic to click the system return key to exit App

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use ionic to click the system return key to exit App, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

In Android, if the event of the system return key is not handled, each click of the return key will return the page to the previous route, which is not in line with the routing logic of app. The correct thing should be: when the page reaches the home page of each navigation page, pressing the return key at this time should prompt whether to exit app, and the user clicks OK to exit app.

Add the following method to the run () method

$ionicPlatform.registerBackButtonAction (function (e) {/ / block the default behavior e.preventDefault (); / / exit prompt box function showConfirm () {var servicePopup = $ionicPopup.show ({title: 'prompt', subTitle: 'are you sure you want to exit the application?' , scope: $rootScope, buttons: [{text: 'cancel', type: 'button-clear button-assertive', onTap: function () {return' cancel' }, {text: 'confirm', type: 'button-clear button-assertive border-left', onTap: function (e) {return' active';}},]}) ServicePopup.then (function (res) {if (res = = 'active') {/ / exit app ionic.Platform.exitApp ();}}) } / / determine whether the current route is the front page of each navigation bar, and if so, display a prompt box if ($location.path () = ='/ index' | | $location.path () = ='/ product' | | $location.path () = ='/ account' | | $location.path () = ='/ more') {showConfirm ();} else if ($ionicHistory.backView ()) {$ionicHistory.goBack ();} else {showConfirm () } return false;}, 101); / 101 priority is often used to override the default behavior of 'return to the previous page'

$ionicPlatform.registerBackButtonAction ()

This method is used to register the system return key event. Each click will only perform the highest priority behavior. For example, when there is a modal box on the page, clicking the system return button closes the modal box instead of returning to the previous view.

Ionic officials have defined the priorities of commonly used behaviors:

Return to previous view = 100

Close sidebar menu = 150

Close Modal=200

Close action sheet=300

Close popup=400

Close loading=500

The usage is as follows:

RegisterBackButtonAction (callback, priority, [actionId])

So when you want to rewrite the behaviors listed above in the ionic official definition, you just need to set the priority higher than the priority of those actions. For example, if you want to override the behavior of returning to the previous view, you only need to pass a value of proirity greater than 100 (and less than 150).

The above is how to use ionic to click the system return key to exit App. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report