How can i do that?
Current solution
I launch a transparent activity, that catches the back press, forwards it to my service and closes itself afterwards. But this activity will be visible in the current running activities and therefore it's not a very beautiful solution.
Solutions seen
I've seen an app that does catch the back press in a service - without an activity that catches the back press. If I show the current running activities, there is nothing from this app.
Question
How can this be done? I've read so many threads that say, this is not possible, but I can see, that there are apps that achieve that somehow...
All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI. Depending on the user's Android device, this button might be a physical button or a software 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.
In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.
If you have an overlay window attached in a WindowManager in your service, put your view the attribute setFocusableInTouchMode(true), and put a key listener. You'll receive key events, like in this example:
view.setFocusableInTouchMode(true);
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
stopSelf();
return true;
}
return false;
}
});
Edited, I didn't use good stackoverflow account
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With