Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-19919] Android: add ability to listen to androidback without overriding

GitHub Issuen/a
TypeImprovement
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2016-06-27T08:00:59.000+0000
Affected Version/sn/a
Fix Version/sRelease 6.0.0
ComponentsAndroid
LabelsCommunity, breaking-change
ReporterManuel Lehner
AssigneeAshraf Abu
Created2015-04-23T11:48:37.000+0000
Updated2017-07-31T18:58:08.000+0000

Description

Currently it is possible to override the default behavior for the back button. But you can't really use this for enhancing this functionality or just listen to the back button event without overriding it. There is no way to call super.onBackPressed() from on the windows activity from JavaScript side. I think it would make sense to add another property to the Ti.UI.Window that lets you explicitly override this behavior. I'd prefer this:
var win = Ti.UI.createWindow();

win.onBack = function(){
  // explicitly override back button behavior
};

win.addEventListener('androidback', function(){
  // just listen for the event without influencing the default behavior
});
Does that make sense? The reason why I need this is that I'm trying to create a proper navigation history. As there are no more lightweight windows, I want to implement a kind of backstack for Views or Fragments.

Comments

  1. Manuel Lehner 2015-05-19

    PR: https://github.com/appcelerator/titanium_mobile/pull/6859
  2. Chee Kiat Ng 2016-01-30

    PR merged.
  3. Manuel Lehner 2016-02-25

    Please note: *this is a breaking change!* win.addEventListener('androidback', onBack); will behave different than before. Apps using this to override the back-button need to update their code.
  4. Ashraf Abu 2016-02-26

    [~manuellehner] Thanks for the heads up. Will get this out of 5.4.0 (aka current Master) and get it merged back when we do 6.0.0. Will be reverting it right now and leave this ticket as opened.
  5. Ashraf Abu 2016-02-26

    PR to revert this for 5.4.0 https://github.com/appcelerator/titanium_mobile/pull/7780 (Merged)
  6. Ashraf Abu 2016-02-26

    Note for when adding this back into 6.0.0: Add docs.
  7. Tim Poulsen 2016-02-26

    I'm not sure I see the use case for this. You can already accomplish custom back button behavior while supporting the stock handling of windows (activities) using code like the following.
       var win = Ti.UI.createWindow(),
       	view = Ti.UI.createView();
       
       win.add(view);
       
       function handleBackButton() {
       	win.removeEventListener('androidback', handleBackButton);
       	// do whatever you want here for custom back button handling
       	// for example, "close" the view
       	view.hide();
       	win.remove(view);
       	view = undefined;
       }
       
       win.addEventListener('androidback', handleBackButton);
       
       win.open();
       
  8. Ashraf Abu 2016-06-27

    PR to get this in https://github.com/appcelerator/titanium_mobile/pull/8087
  9. Ashraf Abu 2016-06-27

    Pr merged
  10. Ashraf Abu 2016-08-04

    Test case is as described in original description.
        var win = Ti.UI.createWindow();
         
        win.onBack = function(){
          // explicitly override back button behavior
        };
         
        win.addEventListener('androidback', function(){
          // just listen for the event without influencing the default behavior
        });
        
  11. Lokesh Choudhary 2016-08-13

    Verified the improvement. Closing. Environment: Appc Studio : 4.7.0.201607250649 Ti SDK : 6.0.0.v20160811221444 Ti CLI : 5.0.9 Alloy : 1.9.1 MAC El Capitan : 10.11.6 Appc NPM : 4.2.7 Appc CLI : 6.0.0-24 Node: 4.4.4 Nexus 5 - Android 6.0.1
  12. Manuel Lehner 2017-07-31

    This does not work for Tabgroup.

JSON Source