Adding a window to the stack
I'm trying to do something pretty simple here: add a new window (containing a webview) to the window stack. The window does open and I can see the webview, but pressing the Android back button closes out the application instead of taking me to the previous window as I would expect. This is my app.js where I'm loading my initial window, and handling an event that opens the new window:
Titanium.UI.setBackgroundColor('#FFFFFF');
// Create root window
var mainWindow = Titanium.UI.createWindow({
backgroundColor: '#FFFFFF'
});
var webview = Titanium.UI.createWebView({
url: 'Home.html'
});
mainWindow.add(webview);
mainWindow.open();
// Event handlers for clicks on the home page
Ti.App.addEventListener('webview_open_window', function(e) {
var web = Titanium.UI.createWebView({
url: e.windowURL
});
var win = Titanium.UI.createWindow({
backgroundColor: '#FFFFFF'
});
win.add(web);
win.open();
});
It seems like the window that I'm creating in the event handler is just replacing whatever is already on the window stack instead of being pushed on top of it. What am I doing wrong?
This is with SDK 1.0 on Android. Thanks!
7 Answers
-
On Android, windows have the concept of being "lightweight" and "heavyweight". Lightweight windows act as containers within the current window exactly like a plain view. The only real difference is that windows can be opened and closed, views can't. To force a heavyweight window in Android you currently need to do one of the following things.
- Provide a url when you open it.
- use the fullscreen or navBarHidden property in the creation option.
The simplest is to do something like.
var win = Titanium.UI.createWindow({ fullscreen : false});
-
Using fullscreen windows for secondary windows seems to be somewhat of a hack and not in keeping with the Android API since it hides the standard Status bar and leaves the application user with a horrible experience.
I found that using
win2.open({modal:true});
will make the second window a heavyweight window and allow the back button to function correctly.Also, the visibility check did not seem to work, but forcing a
win2.close();
before opening the secondary window seemed to do the trick. -
Ok, that works, but then how do I navigate back and forth?
- I open win1 with a button
- press button, on click, open() win2
- press <- to get to win1
- press button, on click, nothing
I tried
button1.addEventListener('click',function(e) { win2.open(); if (!win2.visible) { win2.show(); } });
but this doesnt work.
tia.
-
Dan,
I read on one of the other threads about using a
win2.close()
before thewin2.open()
. I used that in my app and it worked great. -
modal:true
-
Had the same symptoms while debugging an app: Android would quit on closing the window, no matter if I pressed the back button or if I close it myself with a close button calling window.close(), while iOS was working fine (only popping the current window).
Turns out I had exitOnClose:true when I created the window:
var window = Ti.UI.createWindow({ backgroundColor: 'white', exitOnClose: true, fullscreen: true });
Apparently exitOnClose is only working on Android, and if set it's REALLY working :)
Hope this helps.
-
just use: var win = Titanium.UI.currentWindow;