One of my pet hates about Android is the back button. It is fairly unpredictable what it will do.

Depending on where you are, it can do one of three things:

  1. take the user up a page in the same app
  2. take the user back to a previous app
  3. take the user back to the home screen

Normally, I think the 1st and 2nd scenarios generally work pretty well. Even through I'm not sure where it's going to go, 90% of the time, it'll take me to where I want to go.

The last scenario is very frustrating and happens exclusively when I didn't realise that I was already on the first page of my app. It just dumps me on the home screen and I have to figure out how to open the app again. As far as I'm concerned I can't see any good reason for ever taking the user to the home screen when they hit the back screen - especially since there is a home button right next to the back button for that purpose.

Anyway, I want a solution to this. So is there some sort of app or some sort way I can program Tasker or another automation app to either

  1. override and cancel the Back button action if it'll take me to the Home screen. Either have it do nothing or display a message
  2. just disable the back button altogether if clicking it will take me to the home page.

I'm on Lollipop and unrooted.

However, although I want an actual solution for myself, I'd be interested in hearing of any solutions that are rooted or on other versions too.

  • @lucky - it's very unintuitive to me. The user doesn't always know when they're on the main screen, especially if they've come in from another app, and then hit back to go up the stack of the new app. I don't want to have to memorise the entry point of every app. Given there is already a home button - I would much rather just use that instead of the back button. – Diskdrive Jun 6 '15 at 12:00
  • Can you mention an app as an example which just dumps you to home screen, rather than to a previous app? Please provide a practical use-case here. I'm making a solution for days but every time I check this question I get bit confused. // I can provide a solution where, if the app dumps you to home screen then it will be relaunched (optional: with the same parameters, like text or music). But I want to know, do you want this feature only in regard of Home Laucher, or for other previous apps too (I mean do you not want to be dumped (by your running app) to your previous app as well). – Firelord Jun 11 '15 at 2:06
  • @firelord - I don't think an app will ever dump you to the home screen instead of a previous screen. My request is for an app that if there is no previous app, it won't kick me out to the home screen – Diskdrive Jun 11 '15 at 2:10
  • I still need a demo app. All my apps (that are launched from a previous app) returns back to the previous app when pressed back button at main screen of them. None of them goes to home screen unless directly launched from launcher or from home screen. I know I'm close to the solution but I need specifics. – Firelord Jun 11 '15 at 2:48
  • 1
    @firelord - I don't think you'll find an app that'll do that unless the developer has deliberately overridden the back button to do so. But the behavior I'm after I'd to not go to the home screen even if that's the only screen left to go to. I actually what's most frustrating is when you open an app from a notification and you hit the back button taking you back to the home screen and the icon go back to the app isn't even there – Diskdrive Jun 12 '15 at 9:51
up vote 3 down vote accepted

Well, this is still (after investing so many hours) a half-baked (aka partial) solution. You want a message, you'll get a message. :) We need root access for the solution.

Note: Tested the solution on Android 4.2.1 but works on 5.0 as well.


At this point of time, the message will be received as a Toast and it has no effect on Back key.

For the purpose of example I'm using ES File Explorer and BetterBatteryStats to display a message on their default screen.

  1. Fire up Tasker and enable it.
  2. Go to VARS and create a variable as +%Catch (you can use a different name but keep the first letter capital (global variable)). Similarly, create another one with name %Any.
  3. Create a profile with application context and select ES File Explorer.
  4. Create a task under the above profile and then an action inside this task as +Code→ Run Shell and enter these details:

    • Command: dumpsys activity | grep mFocusedActivity | cut -d ' ' -f 6 | cut -d '}' -f 1 -- this command will output the focused activity (current app launched with its activity).
    • Check Use Root.
    • Store Output In: %Catch.
    • Save this action.
  5. Create another profile with application context inverse of ES File Explorer.
  6. Create a task under the above profile and then an action inside this task as +Variables → Variable Set, and enter these details:

    • Name: %Catch
    • To: The (you can put any word)

    This step will make sure that the variable %Catch doesn't hold the value (provided in during execution of step 4) after the profile in step 3 becomes inactive.

  7. Create a last profile under Event → New Window and choose Full Screen under Window Type -- I earlier had Toasts even for dialogs on Main activity. Full Screen option will avoid it.
  8. Create a task under the above profile and then an action inside this task as +Code→ Run Shell, and enter these details:

    • Command: dumpsys activity | grep mFocusedActivity | cut -d ' ' -f 6 | cut -d '}' -f 1 -- this command will output the focused activity (current app launched with its activity).
    • Check Use Root.
    • Store Output In: %Any.
    • Save this action.
  9. Create another action in the same task as +Alert → Flash, and enter these details:

    • Text: Don't back Disk :/ -- this is the message that will be displayed in the Toast. Choose wisely!
    • Tap + corresponding to If and enter %Any eq %Catch -- we're comparing the main activity name for our selected app with any window Android will open. If it matches, you'll see the Toast. Now you can see why we needed an extra "inverse app context" profile in step 5.
  10. Save everything and enable all three profiles.
  11. Note that I had to shift my Superuser app's root access Notifications from Toasts into Status bar notifications because the third profile will need root access every time Android will launch a window. Make sure you either disable your Superuser client's Toasts or change them into Status bar notifications. It was an invasive stream for me!
  12. Anyhow, launch ES File Explorer and see the magic. The default screen of this app will show a Toast to you. You open any other activity and it won't bother you. Come back to main screen of it and see the Toast again.
  13. You can have this behavior for multiple apps. Just choose your app in the profile we made in step 3 and 5. They are supposed to be added in OR context like ES File Explorer or BetterBettaryStats and Not BetterBatteryStats or ES File Explorer.

Some screenshots:

(Click any image for its larger variant)


No toast for any other app                     →                Default screen for BetterBatteryStats                  →

(No toast for any other app)                                                (Default screen for BetterBatteryStats)


No toast on dialog on same screen                    →                  Dialog closed and toast came back on main screen                  →

(No toast on dialog on same screen)                                      (Dialog closed and toast came back on main screen)


Toast on Default screen of ES File Explorer                    →                   No toast on dialog of the same screen                  →

(Toast on Default screen of ES File Explorer)                            (No toast on dialog of the same screen)


Toast came back as soon as dialog disappeared

(Toast came back as soon as dialog disappeared)


Commentary

  • The aforesaid solution didn't work on my device satisfactorily if the app remained in recent list (but OP is not concerned with it) and you happen to open it again.

  • You can get rid of app (and inverse) context based profiles if you use a string in step 9 in place of %Catch like com.estrongs.android.pop/.view.FileExplorerActivity (Main activity of ES File Explorer). The solution will be reduced to two steps. :D

    However, I would like things to be automated here, so why bother especially when you want to select multiple apps for the toast!

What else?

  • Every device more or less has a keylayout under /system/usr/keylayout. In my device the generic layout is named as Generic.kl. The relevant key is mentioned at

    key 158 BACK    WAKE_DROPPED
    

    If you put # in front of it and restart the device then the back key would be disabled. To remain at safe side, I swapped it with Meny key (key 139).

    This swapping can be very beneficial in Tasker. Unfortuntaley, the changes take effect only at next reboot which is very counterproductive for our goal. Let me know if it can be done without a reboot!

  • Xposed Additions(requires Xposed Framework) can remap or disable the Back key et al, but it has no explicitly callable activities (relevant to the goal) which can be utilized by other apps. Also, it doesn't even seem to store a database or a preference file. So I couldn't use it with Tasker to achieve the goal.

  • Secure Settings (another Xposed module) has an option named Kill back button but it seems to be available for Custom ROMs and so, I couldn't test it. Some folks say it does work and I too saw the plugin useful for the goal. Check it out if you've a custom ROM.

I'm open to Ideas here!

I could not figure out the way to find out when an app would dump you to home screen if you press the Back key unless you launched the app from App Launcher. But, I can make sure that the Back key gets disabled if only your app is showing its main/first page. We need Tasker and AutoInput to do the job. The latter gives 7 day trial (full access).

Note: Root access is required for the following method.

First, we need to find out the name of main activity of your app. You can try an app like QuickShortcutMaker to show the main activity of an app.

(Click image to enlarge)

IMG:

The format is <PKG_NAME>/<MAIN_ACTIVITY_NAME> like com.android.chrome/com.google.android.apps.chrome.MAIN

Alternatively, you can use to do the job (Busybox required) provided that you know the package name (PKG_NAME) of the app,

adb shell
dumpsys package PKG_NAME | grep -A1 "android.intent.action.MAIN:" | awk '{print $2}' | tail -n 1 | tr -d '/'

Now that we've got the main activity let's proceed towards Tasker.

Instructions for Tasker and AutoInput

  1. Enable Accessibility service for both apps from Setings → Accessibility.
  2. Go to Tasker → Tasks and create a task named Disable Back Key.
  3. Create following actions inside it:

    1. Code → Run Shell,

      1. Command: dumpsys window | grep "mCurrentFocus" | rev | cut -d '/' -f 1 | tr -d '}' | rev

      2. Check Use Root

      3. Store Output in: %CurrentFocus

      We are simply storing the activity currently in focus.

    2. Task → If,

      • %CurrentFocus eq MAIN_ACTIVITY_NAME

      Change MAIN_ACTIVITY_NAME with the activity name you noted earlier.

    3. Plugin → AutoInput → Modes,

      1. Configuration:

        1. Key Suppress: Enable
        2. Keys: Back
      2. Timeout (Seconds): None

    4. Task → Else → repeat last step but change Key Suppress: Disable.

  4. Create an event based profile from Event → UI → New Window,

    1. Window Type: Full Screen
    2. Priority: Highest
  5. Link the task Disable Back Key in it.

  6. Rename the profile to AutoInput disable Back Key.
  7. Create a separate new task named Enable Profile.
  8. Create action 3 of step 3.
  9. Create another action from Tasker → Profile Status:

    • Name: choose AutoInput disable Back Key
    • Set: On
  10. Create one more task named Disable Profile.

  11. Repeat step 9 but change Set: Off.
  12. Create action 3 of step 3 but change Key Suppress: Disable.
  13. Create an application based profile from Application → YOUR_APP,

    1. Add Enable Profile as Enter Task
    2. Add Disable Profile as Exit Task
  14. Deactivate AutoInput disable Back Key profile because we don't want to use CPU all the time in processing this profile on every new window formation by Android.

This is it. Launch the app you selected earlier and you would find that the Back key doesn't work when the main activity of your app is opened. Go to any other activity of the app like preferences or settings and Back key would come back to normal usage.


There is somewhat good news and bad news for owners of non-rooted Lollipop devices.

Good news: The said method can work without root access as well. running in wireless mode and connected in loopback can do the job.

Bad news: There are too many booby traps in using it and I cannot (read: don't have time) work on to take care of them.

Do post an answer if you get rid of issues with .

The third scenario is the case where you hit the back button when you are the main screen of an app. Some app or games confirms the exit of the app with yes or no option..some do not have that..it will bring you back to the menu drawer or the home screen if you have launched it from the home screen or notification bar. So thats normal. If you are in the main activity screen of an app I would expect to show the home screen if i press the back button. Thats seems intuitive for me. And if back button exits an app without any confirmation you can still find it in the recents tab.

However in Lollipop you have Screen Pinning option available under Settings-> Security which allows you to stay in that particular app and back button will be disabled until you unpin the application. And you wont be able to come back to the home screen on pressing the back button from that application. But you can't switch from one application to another application without unpinning the current app.

If you feel you dont require the default back button actions anymore, Here is a related app in PlayStore. Back Button(No Root). You can turn off/disable the virtual or navigation bar back button completely if you want to using this app. The floating bar back button work fine in Lollipop but I was not able to enable the Navigation bar since it doesnot work on android versions 4.4.4 or later.

Screenshot Screenshot_1 Screenshot_2

Screenshot (Click to enlarge)

  • Hi Lucky! How do you use Back Button app to completely disable back button (service turned on already)? All I could see is it shows a soft key for back button. The reviews tends to say the same. – Firelord Jun 6 '15 at 22:43
  • You have to turn off the back button option in that app..it will open the virtual navigation bar with no back button..only recent and home key will be displayed.. – Lucky Jun 6 '15 at 22:55
  • Would you mind sending the screenshots of the settings? – Firelord Jun 6 '15 at 22:58
  • You can see it in the application menu under the floating bar and the navigation bar tabs..just uncheck the back button and you will not be able to see it after activating..but the navigation bar doesnot work in kitkat and lollipop..only the floating bar i can able to hide the back button.. – Lucky Jun 8 '15 at 5:52

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.