15 Replies Latest reply on Jul 16, 2014 9:28 AM by Nicolas Hinze

    Disable Android back button

      Hello,

       

      We need to disable Android back button on Rhomobile Application. We found documentation about "Redefine back action" on Rhomobile Docs, but it doesn't solve our problem because it's about Rhomobile menu back button and we need to disable physically Android back button.

       

      Thanks.

       

      Jordi.

        • Re: Disable Android back button

          are you talking about the back button in the app or on the device?

          • Re: Disable Android back button

            I don't believe this physically possible with Rhodes.  In an actual android app you can define what the 'Back' button does, but in Rhodes this appears to go to the main window as a browser back action.

            • Re: Disable Android back button
              Dave van der Veen

              If you locate the sourcefile RhodesActivity.java you can change the method onKeyDown to change the back behaviour.

               

              @Override
                  public boolean onKeyDown(int keyCode, KeyEvent event) {
                      switch (keyCode) {
                      case KeyEvent.KEYCODE_BACK:
                          RhodesService r = RhodesService.getInstance();
                          if (DEBUG)
                              Logger.D(TAG, "onKeyDown: r=" + r);
                          if (r == null)
                              return false;
                          
                          //Comment the following two lines to prevent the back behaviour
                          MainView v = r.getMainView();            
                          v.goBack();//back(v.activeTab());
                          return true;
                      }
                      
                      return super.onKeyDown(keyCode, event);
                  }
              

               

              Check the following link for more information on this event.

              http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29

               

              For me this was the only possible way the accomplish it.

              • Re: Disable Android back button

                Redefining back action in rhodes really redefines all the possible back related UI: 'back' menu, 'back' native bar button and 'back' android physical key/action bar button.

                Can you report that it does not work for Android physical key?

                  • Re: Disable Android back button

                    Regarding to the Android back button,

                    I have the following controller

                     

                     

                     

                    1. class TodoController < Rho::RhoController 
                    2.   include BrowserHelper 
                    3.   def index 
                    4.      render :back => url_for(:controller => :Home, :action => :index
                    5.    end 
                    6.  
                    7.   def new 
                    8.      render :back => url_for(:controller => :Todo, :action => :index
                    9.    end 
                    10. end 

                    If pressing back button in New Todo view, the app navigate to the view of Todo Index. However, if I press back again, it not navigate to the Home Index view, but returning to the New Todo view.

                    I'm using Rhodes 3.5.1. Please help if you know anything about this problem.

                  • Re: Disable Android back button
                    Graham Bird

                    Was there ever a solution to this issue? As far as I can tell the issue is still outstanding.

                     

                    We have been experimenting to try and ensure that the hardware "Back" button on the Android device does not break our desired program flow.

                    We have a controller that has the line:

                    render :action => :new, :back => url_for(:controller => :Run, :action => :index)

                    The page is opened from a callback on a page we do not want the user to return to, but when the back button is pressed the user is returned to the unwanted page instead of being taken to the Run Index page specified above.

                     

                    I have been trying a great variety of methods to resolve the issue, including trying to use the pagebeforechange event to capture the issue, but none of these have provided a suitable solution.

                      • Re: Disable Android back button
                        Hein Boshoff

                        Make android back button do nothing like this:


                        def index

                          ...

                          render :back => 'callback:' + url_for(:action => :callback_alert)

                        end

                         

                        def callback_alert

                           # do nothing

                        end

                          • Re: Disable Android back button
                            Graham Bird

                            Hein,

                            Thanks for your reply, but unfortunately this option does not work.

                            All this does is redirect the software "back" button (which we had already applied) but the Android hardware back button is overriding the behaviour specified and taking the user back to their previous page.

                            I did run a test using your code above since we were not using "callback" so it was feasible that this behaved in a different manner. The test was unsuccessful.

                             

                            What this means is that even if you specify workflow logic in the application the user can still return to a previous page by pressing the hardware button.

                            We have already plugged this gap so that logging out and then pressing back does not allow the user back into the application (this is the biggest issue it causes) but it is not a simple task to check for all possible scenarios where the user may have redirected incorrectly.

                             

                            We have raised this issue to Motorola and they have raised an internal ticket for it so hopefully we will see a solution at some point.

                        • Re: Disable Android back button
                          Jared Thigpen

                          This may not be useful in a situation where you are supporting a slew of Android devices, but you can use the Rho API to override behavior associated with a specific hardware back button. For example, on the MC40, the string value for the back button is "4". So you can use the API to intercept that key press and route it into your own callback.

                           

                          Rho::KeyCapture.captureKey(false, "4", "/app/System/navigate_back")

                           

                          As I said, this may not be useful for people that aren't supporting a limited and specific set of Android devices, but I thought I'd share as this is in fact how we disable the hardware back button in our applications.

                            • Re: Disable Android back button
                              Graham Bird

                              Jared,

                               

                              Many thanks for this.

                              We've put in the KeyCapture.captureKey handler and it allows us to block the hardware back button on the device.

                              Rhomobile | KeyCapture

                              The code for Android devices is also "4".

                               

                              For some reason I was unable to get the Javascript version of this to work but by putting the handler into application.rb we are able to put in a global handler for the event.

                              At that point it is possible to manufacture a more bespoke control mechanism for the pages and we will be looking into that going forwards.

                            • Re: Disable Android back button
                              Nicolas Hinze

                              In Rhomobile 4.1.1 there is an undocumented option you can set in your rhoconfig.txt to disable the back button on Android:

                               

                              Add this line to rhoconfig.txt of your app:

                               

                              # disable Android back button

                              disable_back_button = 1