Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

How to fire android:back or simulate back button click

Is there a way to programmatically trigger an android:back button or simulate as if the user has clicked the back button?

— asked April 27th 2011 by Jake Jake
  • android:back
3 Comments
  • Hi Jake.

    You can use "fireEvent" to trigger event.

    Ivan

    — commented April 27th 2011 by Ivan Škugor
  • Thanks Ivan, do you know on what object you fire the event? I've tried Ti.App.fireEvent as well as the current window, ie win.fireEvent('android:back'). but neither seem to trigger the back-button behavior.

    — commented April 27th 2011 by Jake Jake
  • If your intent is to add iphone like functionality ( i.e. "back" button in the nav bar), I modified NavigationControler.js to add

    //go back function to simulate iPhone nav bar back button in Android view
    exports.NavigationController.prototype.back = function() {
        //store a copy of all the current windows on the stack
        var windows = this.windowStack.concat([]);
        var lastwindow = windows.length -1;
        if(windows.length>1){
            (this.navGroup) ? this.navGroup.close(windows[lastwindow]) : windows[lastwindow].close();
        }
    
    };
    

    then just add a button where ever you want your "back" button…

        var backButton = Ti.UI.createButton({
            title:'Android Back Button',
            height:'50dp',
            width:'200dp',
            top:'20dp'
        });
        win.add(backButton);
    
        backButton.addEventListener('click', function(){
            navController.back(); 
        });
    
    — commented March 18th 2013 by Mark Stipich

3 Answers

  • Jake

    There is indeed a way, and it is described in ticket TIMOB-1641. :)

    Cheers

    — answered April 27th 2011 by Paul Dowsett
    permalink
    3 Comments
    • Jake

      Could you explain what you are attempting to achieve? The Android back button allows the user to interact with the application, rather than the application to interact with the user.

      If you wish to code the behavior of your app when the user presses the Android back button, then you can listen to its event using the information provided in the link I posted. To be clear, here's a usecase:

      Titanium.UI.setBackgroundColor('white');
      
      var window1 = Ti.UI.createWindow({
        backgroundColor:"red",
        navBarHidden:false,
        title:"Main Window"
      });
      
      window1.addEventListener('click', function(){
      
        var window2 = Ti.UI.createWindow({
          backgroundColor:"green",
          navBarHidden:false,
          title:"Sub Window"
        });
      
        window2.addEventListener('android:back', function(e) {
          Ti.API.info("Log: The Android back button was pressed - DO SOMETHING!!!!");
       // when overriding the back button, the default behavior
       //   (in most cases, close) needs to be explicitly coded 
          window2.close();
        });
      
        window2.open();
      
      });
      
      window1.open();
      

      By default, the back button closes the current window. To close a window programmatically, simply use the Titanium.UI.Window close method. In the above example, I closed window2 like this:

      window2.close();
      

      alternatively, you could use:

      Ti.UI.currentWindow.close();
      

      I may have misunderstood something. If so, please explain your objective, and I or someone will help further.

      Cheers

      — commented April 27th 2011 by Paul Dowsett
    • Thanks Paul - there seems to be something slightly different when clicking the back button vs closing the window, I am just trying to simulate that.

      If you just call window.close(), it seems to behave differently than when clicking the back button. For example, in a NavigationGroup on the iPhone, the title bar contains a back button. when you click it there is a specific transition animation that occurs and it "slides" back to the previous view on the stack. when you close the window, it just disappears.

      It also seems to have some actual functionality differences on the Android - I apologize that I don't remember what it was, but again the animation is changed.

      So I've just been trying to figure out what really happens when you click the "back" button and how to simulate that programmatically, because there does seem to be more to it than just closing the window.

      — commented May 9th 2011 by Jake Jake
    • ps - you can set window.close({animate: true}) and it does animate on the iPhone however if you look it still is not the same animation! The titlebar slides differently.

      It seems to have something to do with popping a view off the stack vs closing a window. I know it seems minor but it's just one of those little details!

      thanks again, j

      — commented May 9th 2011 by Jake Jake
  • I maybe late … to update this here as i see it hasn't been updated since months now. But still this was the first link i came across when searching for a solution. And this is what i did.
    Firstly the android:back button actually does more than closing the app. A good read on the action of back button is this THIS.
    Add the below line to the "back button"(your custom made button) event listener.

    var activity = Titanium.Android.currentActivity;
        activity.finish();
    
    backButton.addEventListener('click', function(){
        var activity = Titanium.Android.currentActivity;
        activity.finish();
    
    });
    

    This works in my case

    — answered July 10th 2012 by Anand Kumar Singh
    permalink
    3 Comments
    • Works like a charm!

      — commented March 13th 2013 by Christoph Köpernick
    • Brilliant! This is exactly what I had been looking for! It's always so simple once you know! Thanks dude!

      — commented May 11th 2013 by Robin Williams
    • This closes an application for me.

      — commented February 6th 2015 by Igor Kravchenko
  • A number of people do not know that the "android:back" window event is not triggered unless the window has "navBarHidden:true" specified. Please ensure that this property is set and then try it out.

    — answered February 28th 2012 by Sameer Prabhu
    permalink
    2 Comments
    • I maybe late … to update this here as i see it hasn't been updated since months now. But still this was the first link i came across when searching for a solution. And this is what i did.

      Firstly the android:back button actually does more than closing the app. A good read on the action of back button is this THIS.

      Add the below line to the "back button"(your custom made button) event listener.

      var activity = Titanium.Android.currentActivity;
          activity.finish();
      
      backButton.addEventListener('click', function(){
          var activity = Titanium.Android.currentActivity;
          activity.finish();
      
      });
      

      This works in my case

      — commented July 10th 2012 by Anand Kumar Singh
    • This closes the application instead of taking it back to previous page.
      What i might be doing wrong?

      — commented April 20th 2013 by monu kumar
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.