A SERVICE I HAVE NOT ANY ACTIVITY is running in forebackground and when user click on app button i want do function of hardware back button
onBackPressed
is not working inside service!!
how to press back button programmatically in android
8.3k views Asked by dornadev At
4
There are 4 answers
1
On
You can handle back button operation like this;
@Override
public void onBackPressed() {
// your code.
}
otherwise try below code;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
1
On
Currently, it's not possible to press "BACK" from service. Since you don't have an activity in the foreground, you don't have the ability to programmatic to press a BACK press, due the fact the service not provide any user interface. What is the option - If you have a rooted device with an access to shell you can try to use the following command in the service Process process = Runtime.getRuntime().exec("adb shell input keyevent 4");
Is it not possible to programmatically press the back button on android through a service
you need to just call
onBackPressed()
method don't forgot overrideonBackPressed()
in your activity