Titanium Community Questions & Answer Archive

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

Stop the back button closing app on Android

I'm having real trouble structuring the app to make the windows behave like I describe in this Q.

I'm now trying to go back to basics. Here's a 2 level approach (no tabs, no navGroup). On Android it displays the second level, but hitting the 'Back' button closes the app.

I'm sure the solution is simple, but I've been trying different things for a few hours and haven't gotten anywhere with it :(

app.js

var mainMenu = Ti.UI.createWindow({
  backgroundColor:"#fff",
  title:"My App"
});
var label = Ti.UI.createLabel({ text: "poke me to open the next window" });
mainMenu.add(label);

label.addEventListener("click", function(e) {
    win = Titanium.UI.createWindow({
        url:'app-sub1.js',
        title:'second'
    });    

    win.open();
});

mainMenu.open();

app-sub1.js

var win2 = Titanium.UI.currentWindow;

var secondView = Titanium.UI.createView({
    width:261,
    height:178,
    top:20
});

var label = Ti.UI.createLabel({ text: "second window" });
secondView.add(label);
win2.add(secondView);

Any ideas?

Thanks in advance,

Matt.

— asked June 22nd 2010 by Matt Collinge
  • android
  • back
  • navigation
0 Comments

4 Answers

  • Accepted Answer

    An Android 'back' will close 'heavyweight' windows. Since you're creating a lightweight window back closes your app instead. Have a search for 'heavyweight' to see previous threads on this topic.

    — answered June 22nd 2010 by Damien Elmes
    permalink
    2 Comments
    • So is it not possible to create lightweight windows and still use the backbutton to navigate through them?

      — commented July 1st 2011 by Julian Lindblad
    • Is there a way to do this for the iphone? Currently, if I do something like this:

      // this is the main window of the app.. aka the root window and cannot be closed.
      var win = Titanium.UI.currentWindow;
      
      //create a window that I want to close, which all elements on this page (views, buttons etc) can be added to this window. This window is added to the root window.
      var inner_window = Ti.UI.createWindow
      ({
          backgroundColor:'transparent' //set background to transparent so its just pasted on the white background of the root window
      });
      inner_window.open();
      win.add(inner_window);
      

      if i now call inner_window.close() anywhere… the app itself closes and not the window. (NOT GOOD)! and really dont know how to fix this. Some of the pages I'm using… well it just doesn't make sense to use them in a tabGroup.. I just simply want to close the window.

      — commented June 19th 2013 by Geremy Farr-Wharton
  • Actually, you can capture the back button click. When you do this, it no longer closes the current heavyweight window. See answer here: http://developer.appcelerator.com/question/118993/how-to-fire-androidback-or-simulate-back-button-click

    — answered July 20th 2012 by John Gould
    permalink
    0 Comments
  • You can't add one window to another window, as far as I know (or at least you don't need to). When you call inner_window.open(), doesn't that open the window right on top of win? Then when you call inner_window.close(), it should just close inner_window and leave win in place.

    — answered June 19th 2013 by John Gould
    permalink
    0 Comments
  • modal:true,
    exitOnClose: false

    this fixed my problem, Thanks

    — answered October 18th 2013 by Sumair Saqib
    permalink
    0 Comments
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.