Is there a way I can prevent the back button from going to the home screen? 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:


take the user up a page in the same app
take the user back to a previous app
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


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
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.
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.


Fire up Tasker and enable it.
Go to VARS and create a variable as +&rarr; %Catch (you can use a different name but keep the first letter capital (global variable)). Similarly, create another one with name %Any.
Create a profile with application context and select ES File Explorer.
Create a task under the above profile and then an action inside this task as +&rarr; Code&rarr; 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.

Create another profile with application context inverse of ES File Explorer.
Create a task under the above profile and then an action inside this task as + &rarr; Variables &rarr; 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.
Create a last profile under Event &rarr; 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.
Create a task under the above profile and then an action inside this task as +&rarr; Code&rarr; 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.

Create another action in the same task as + &rarr; Alert &rarr; 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.

Save everything and enable all three profiles.
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!
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.
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)



 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&rarr;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&rarr;

(No toast for any other app) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&rarr;

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





(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!