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)
The format is <PKG_NAME>/<MAIN_ACTIVITY_NAME>
like com.android.chrome/com.google.android.apps.chrome.MAIN
Alternatively, you can use adb 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
- Enable Accessibility service for both apps from Setings → Accessibility.
- Go to Tasker → Tasks and create a task named
Disable Back Key
.
Create following actions inside it:
Code → Run Shell,
Command: dumpsys window | grep "mCurrentFocus" | rev | cut -d '/' -f 1 | tr -d '}' | rev
Check Use Root
- Store Output in:
%CurrentFocus
We are simply storing the activity currently in focus.
Task → If,
%CurrentFocus
eq
MAIN_ACTIVITY_NAME
Change MAIN_ACTIVITY_NAME
with the activity name you noted earlier.
Plugin → AutoInput → Modes,
Configuration:
- Key Suppress: Enable
- Keys: Back
Timeout (Seconds): None
Task → Else → repeat last step but change Key Suppress: Disable.
Create an event based profile from Event → UI → New Window,
- Window Type: Full Screen
- Priority: Highest
Link the task Disable Back Key
in it.
- Rename the profile to
AutoInput disable Back Key
.
- Create a separate new task named
Enable Profile
.
- Create action 3 of step 3.
Create another action from Tasker → Profile Status:
- Name: choose
AutoInput disable Back Key
- Set: On
Create one more task named Disable Profile
.
- Repeat step 9 but change Set: Off.
- Create action 3 of step 3 but change Key Suppress: Disable.
Create an application based profile from Application → YOUR_APP,
- Add Enable Profile as Enter Task
- Add Disable Profile as Exit Task
- 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. adb 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 adb.