Wednesday, December 30, 2015

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:



  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.



Answer



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!


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...