Back hardware button android

Hardware Back Button

The hardware back button is found on most Android devices. In native applications it can be used to close modals, navigate to the previous view, exit an app, and more. By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed. If no previous view exists in the navigation stack, nothing will happen. This guide will show how to customize the behavior of the hardware back button.

The hardware back button refers to the physical back button on an Android device and should not be confused with either the browser back button or ion-back-button . The information in this guide only applies to Android devices.

Hardware Back Button in Capacitor and Cordova

When running in a Capacitor or Cordova application, Ionic Framework will emit an ionBackButton event when a user presses the hardware back button.

When listening for the ionBackButton event, you can register a handler to be fired. This handler can perform actions such as quitting the app or opening a confirmation dialog. Each handler must be assigned a priority. By default, only one handler is fired per hardware back button press. The priority value is used to determine which callback should be called. This is useful because if you have a modal open, you likely would not want the modal to close and the app to navigate backwards when pressing the hardware back button. Only running one handler at a time allows the modal to close but still requires another press of the hardware back button to navigate backwards.

There are situations where you might want to have multiple handlers fired. Each handler callback passes in a function as a parameter that can be used to tell the framework to call the next handler.

Hardware Back Button in a Browser

When running your app in a mobile browser or as a PWA, hardware back button customization will be limited. This is because Capacitor and Cordova expose additional features that are not exposed in a normal web browser. For example, closing overlays and menus via the hardware back button are functionalities that are currently not supported when running your app in a mobile browser. These are known limitations and do not currently have straightforward solutions.

For complete hardware back button support, we recommend using Capacitor or Cordova.

The ionBackButton event will not be emitted when running an app in a browser or as a PWA.

Basic Usage

In this example, we are registering a handler to be called when the hardware back button is pressed. We have set the priority to be 10, and we have not indicated to the framework that we want the next handler to be called. As a result, any handlers with a priority less than 10 will not be called. A handler that has a priority greater than 10 will be called first.

In the event that there are handlers with the same priority value, the handler that was registered last will be called. See Handlers with the Same Priorities for more information.

Calling Multiple Handlers

Each hardware back button callback has a processNextHandler parameter. Calling this function allows you to continue calling hardware back button handlers.

This example shows how to indicate to Ionic Framework that you want the next handler to be fired. All callbacks are provided with a processNextHandler function as a parameter. Calling this will cause the next handler, if any exists, to be fired.

Handlers with the Same Priorities

Internally, Ionic Framework uses something similar to a priority queue to manage hardware back button handlers. The handler with the largest priority value will be called first. In the event that there are multiple handlers with the same priority value, the last handler of the same priority added to this queue will be the first handler to be called.

Читайте также:  Буфер обмена андроид где искать

In the example above, both handlers A and B have a priority of 10. Since handler B was registered last, Ionic Framework will call handler B before it calls handler A.

Exiting the App

In some scenarios, it may be desirable to quit the app when pressing the hardware back button. This can be achieved through the use of the ionBackButton event combined with methods that Capacitor/Cordova provide.

This example shows the application exiting when the user presses the hardware back button and there is nothing left in the navigation stack. It is also possible to display a confirmation dialog before quitting the app.

It is recommended to check whether or not the user is on the root page prior to exiting the application. Developers can use the canGoBack method on IonRouterOutlet in Ionic Angular and IonRouter in Ionic React and Ionic Vue.

Internal Framework Handlers

The table below lists all of the internal hardware back button event handlers that Ionic Framework uses. The Propagates column notes whether or not that particular handler tells Ionic Framework to call the next back button handler.

Источник

Detecting the Android Back button in NativeScript

The Android hardware Back button can be a pain to handle. By default it goes back to the previous route, and when route history ends, it exits the app. It’s not necessarily what you’d like. For example when you open a modal dialog, you may want the Back button to close it instead of navigating away from the parent page. You may want to display a confirmation dialog before doing that.

Abducting the Back button event
First of all you’ll need an application-wide event listener. I personally prefer to put it into a service called AppService which handles everything related to the app, the OS and the device itself, but app.component.ts may serve just as fine. The point is to run it once on startup.

Create this method:

Call it from the constructor() or ngOnInit() but don’t forget to check if your app is running on Android:

What have we just achieved? Upon launching our app, when our singleton service initializes, it’ll create an event listener and watch the Back button. If the currently viewed page has its own event listener looking for the BackButton event, it cancels the default action tied to the Back button and notifies the page component that a BackButton event has occurred. The page then does whatever it has to do.

In your page component put this into the ngOnInit() method:

Thus upon pressing the hardware Back button, the doThing() method will be triggered. The second parameter passes the context to the method, in this case this so you can access variables and other methods from inside that method.

Remove the listener in your ngOnDestroy() :

It may not cause a problem if you don’t remove it, as it won’t trigger unless the page is in view anyway. But you absolutely have to disable the listener before opening modal dialogs because pressing the Back button over a modal would trigger the listener in the parent component. In this case disable it before calling ModalDialogService.showModal() and reinitialize it upon in the callback.

At the same time keep in mind that NativeScript handles Angular lifecycle hooks differently from Angular on the web! ngOnDestroy() is not called automatically when navigating away from a page. See my other article on this topic: The curious case of Angular lifecycle hooks in NativeScript.

You may also notice that the Back button sometimes triggers more than once. It’s one of those mysterious bugs which take a lot of your precious time at the worst moments. The bug is actually not in your code but in NativeScript LiveSync. As you change and save your code and LiveSync refreshes the app in the emulator, the listener initiated by the previous build persists. As a result if you refreshed the app nine times, you’ll get nine button presses. Of course why, oh why would this be documented anywhere. So just don’t use LiveSync when tinkering with Android hardware events, and remember those happier times when development tools were straightforward and reliable.

Читайте также:  Смс утилита для андроид

Источник

How to Remap the Hardware Buttons On Your Android Phone

The hardware buttons on your Android phone are already programmed to be useful, but if you’re repurposing an old phone as something new, want to switch the buttons around to something you’re used to, or just enjoy bending Android to your will, it’s not hard to change them. Let’s take a look at how to remap the buttons on your Android device, and why you might want to.

There are a ton of reasons you might want to remap the buttons on your Android phone or tablet. If you’re using it as a remote control, it’s easier to just assign the hardware buttons to playback controls so you don’t have to wake, unlock, and open your remote control app every time you want to skip commercials or play the next episode in your playlist. If you’re using your smartphone as a camera, setting a hardware button to the shutter is easier than tapping the screen—and it opens the door to using external triggers to take pictures.

It’s also possible—especially in the case of Samsung devices—that your buttons have been mapped to Samsung’s own apps and shortcuts, pushing Google’s own versions into the background (for example, double-tapping Home on the Note 3 and many other Samsung devices opens S-Voice, as opposed to Android’s own voice search.) Alternatively, maybe you have an older Android phone with a search button on it, and you want that button to do something other than search—maybe take a picture, or fire up your music player, or open Navigation. It’s all easy to do. Here’s how.

Use ButtonRemapper to Reassign the Hardware Buttons to New Commands

Apple AirPods 3

A whole new look

ButtonRemapper has been around for a long time , and while it’s a stretch to say it’s still under development, it definitely still works on many devices. You’ll need to be rooted , and you can grab it over at the XDA Developer Forums for free. Once installed, you can set any of your phone’s physical buttons to commands like power, camera, volume up or down, play/pause (and other specific media playback commands), and of course, any of the basic commands like menu, back, home, or search. If your device has one of those buttons set to something else, you can switch it back to the Android default, or you can set the buttons to do anything else you wish.

Everything You Need to Know About Rooting Your Android Phone

We love Android, but rooting your phone can give you the opportunity to do so much more than your…

One of the nice things about ButtonRemapper is that you can control whether the button passes its command to the system when the phone is asleep or not. Set a button’s commands to «WAKE,» and the command will both wake the phone and pass the command through. Set it to «WAKE_DROPPED,» and the phone will wake up when you tap the button, but the command will get dropped. Still, if you have your phone set up as a remote control, configuring the physical buttons at the bottom to play/pause, forward, and back, then setting the volume sliders to the volume on your TV, and finally setting them all to WAKE will make sure that even if your remote is asleep, you can still control what’s happening on the big screen.

Like we said though, ButtomRemapper has been around for a long time. Your odds of it working smoothly go up if your phone is a little on the old side, and don’t expect it to work particularly well with newer devices that use software buttons for back, home, and app switching at the bottom of the screen like the Moto X does, for example. Some devices require special versions too, like this one for the Galaxy Tab . Check the thread for details, and see if your phone is supported. If you want to edit keymap files on your own, there’s even a utility in the first post to help you with that too.

Читайте также:  Андроид для самсунг gt p7500

Double-Tap the Home Button to Launch Specific Apps

If you don’t want to tweak the individual buttons at the bottom of your phone, or you’d rather use them as shortcuts instead of reprogram them, try Home2 Shortcut , a free app at Google Play. Over at ComputerWorld , JR Raphael uses it to change the way Samsung devices default to S-Voice instead of Google’s own Android voice search when you double-tap the home button. If you want to do the same for your Samsung device, or you just want a system-wide shortcut for Voice Search or Google Now, Home2 Shortcut can do it.

Once installed, you can set shortcuts for any of your system’s hardware buttons, not just the Home button. Home2 Shortcut can set an app to launch when you double-tap home, but it can also set an app to launch when you tap home and then search, or home and menu, home and search, and more. You can even tell it to launch a specific app when you long-press Search or Camera (if your phone has either button).

Add Customizable Buttons to Your Screen Instead

If you don’t like the idea of fiddling with all of this, or you have remapped your buttons but still want access to the standard commands, try Button Savior . You might remember it from when we rooted a Nook Simple Touch —it’s the app that, once installed, lets you customize which hardware buttons on the device do what, and adds a translucent overlay on top of the screen with home, back, menu, and a few other buttons that you can always access, no matter what app you’re in.

Turn a $99 Nook into a Fully Fledged Android Tablet in Four Easy Steps

The Nook Simple Touch is great for reading ebooks. It’s got plenty of storage, it’s small enough…

Button Savior works the same way on your phone—the buttons may be a little tough to see, but if you’ve remapped your hardware buttons, you can use it to get an overlay with those old commands so you don’t have to use crazy workarounds to get back to the home screen or open the app drawer. Best of all, the overlay is completely customizable, so you can add as many buttons as you choose. The only downsides to Button Savior is that it requires root (although there is a non-root version ), and it only works up to Android 4.0 (Ice Cream Sandwich). This makes it ideal for those old phones you may be repurposing into remote controls, transforming into home automation projects, or using as cameras. $2 gets you the pro version , with a few extra features.

If you need another alternative, try Virtual Button Bar . It’ll set you back $2, but it supports Android 2.2+ (including ICS and Jelly Bean), and offers similar features. It still requires root and has some quirks on Samsung devices, but it’s a solid option too.

Of course, if you’re perfectly happy with the button mapping on your device, you don’t have to change a thing, but if you just got a new phone and want to turn the old one into something interesting, or you do like tinkering and tweaking so everything works just the way you want it to, you have plenty of options.

Источник

Оцените статью