Why APP is exiting on back button Titanium - javascript

app.js
if (osname === 'android') {
Window = require('ui/handheld/android/SignIn');
}
else {
Window = require('ui/handheld/SignIn');
}
new Window().open();
SignIn.js
function SignIn() {
var self = Ti.UI.createWindow();
//Some design and sign-in validation code
...
var StatusMain = require('/ui/handheld/android/StatusMain');
new StatusMain(global_vars).open();
return self;
}
StatusMain.js
function StatusMain(global_vars) {
var self = Ti.UI.createWindow();
return self;
}
On StatusMain.js, screen When I click on device's back button APP exits instead of going back on SignIn.js screen
Any help will be highly appreciable!
Thanks in advance,
Mohsin

You can handle back button event like this in your code
window.addEventListener('android:back', function(){
// close your current window
});

I suggest you set the (Android specific) exitOnClose property on false when creating the new window:
http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.Window-property-exitOnClose
exitOnClose : Boolean
CREATION-ONLY
Boolean value indicating if the application should exit when the Android Back button is > > pressed while the window is being shown.
You can only set this as a createWindow({...}) option. Setting it after window creation > has no effect.
StatusMain.js
function StatusMain(global_vars) {
var self = Ti.UI.createWindow({
exitOnClose: false
});
return self;
}
That should do the trick. Although the default value is false, it seems that your issue has something to do with that. I recommend experimenting with settings this property to true/false.
Word of advise, you should also test your app on a device if you haven't done so already. My experience with the Android emulators is rather inconsistent at some points.

StatusMain is a lightweight window. It doesn't create new Android activity, instead it shares with SignIn window activity. That's why your app closes when press back button. You need to create a heavyweight window to (specify fullscreen or navBarHidden properties) StatusMain window.

Set the modal property for the new window to true.
function StatusMain(global_vars) {
var self = Ti.UI.createWindow({
modal: true
});
return self;
}

Related

Unable to switch focus to child window in Safari browser using Protractor

Unable to switch focus to Child window in Safari browser version 11 and 12
OS version : macOS High Sierra and macOS Mojave
Below is the code snippet that I have tried.
exports.switchWindowFocus = function (url) {
var self = this;
browser.getAllWindowHandles().then(function (handles) {
browser.wait(self.windowCount(2), 10000);
browser.switchTo().window(handles[1]).then(function(){
expect(browser.getCurrentUrl()).toEqual(url);
})
});
};
Still the focus remains on the main window instead of switching to child window.
Can anyone please help me with this?
I was surprised to see the browser.wait(self.windowCount(2)) inside the browser.getAllWindowHandles(). If you get all the handles and it wasn't there yet, then waiting for it afterwards won't do much good. Also, the browser.wait should probably be promise chained with whatever comes after. Maybe something like (untested):
exports.switchWindowFocus = function (url) {
var self = this;
browser.wait(self.windowCount(2), 10000).then(function() {
browser.getAllWindowHandles().then(function (handles) {
console.log(handles.length);
browser.switchTo().window(handles[1]).then(function() {
expect(browser.getCurrentUrl()).toEqual(url);
})
});
});
};

Protractor cannot find element after re-defining browser instance

I am trying to use two browser instances in one test. After forking new driver instance and opening url in new window, protractor cannot find element on that window, even though elements are definitely visible.
Here is my code sample:
app.js
var app = {
browser: undefined,
useBrowser: function (b) {
this.browser = b;
return this;
},
useBrowserAlpha: function () {
return this.useBrowser(browsers.alpha);
},
useBrowserBeta: function () {
return this.useBrowser(browsers.beta);
},
}; module.exports = app;
Then, in test file I use it like this:
it('Test', function () {
browser.ignoreSynchronization = true;
app.openGoogle();
element(by.css('#lst-ib')).sendKeys('Hello');
browsers.beta = browser.forkNewDriverInstance();
app.useBrowserBeta();
app.openGoogle();
element(by.css('#lst-ib')).sendKeys('Hello');
});
In this test, first browser instance is opened and navigates to www.google.com, then types word 'Hello' in search bar, then second instance of browser is initiated, also opens www.google.com and then protractor sends keys to first browser search bar again. Any ideas on how it can be resolved will be highly appreciated.

How to refresh the parent page on closing the rad window

I have a opened a rad window using below java-script code given below. Now I want to refresh my parent page on closing the rad window. To achieve this i have linked a jquery method, OnSendInviteClose with onclose event. Now this code refreshes my parent page but some time it gives me error described as "can't execute code from a freed script". How to fix this issue or reload my parent page on close (any other way).
Thanks
var e_showaddresslist = function (sender, args) {
var url = $page.url.create("Pages/CalendarInvites.aspx?parentScreen=sendInviteWnd");
var win = $window.createPopup(url,
{
size: "750, 500", behaviors: Telerik.Web.UI.WindowBehaviors.Close | Telerik.Web.UI.WindowBehaviors.Maximize | Telerik.Web.UI.WindowBehaviors.Move | Telerik.Web.UI.WindowBehaviors.Reload | Telerik.Web.UI.WindowBehaviors.Modal, name: "sendInviteWnd", onclose: onSendInviteClose
},
function () {
//$page.get_window().set_destroyOnClose(true);
});
}
function onSendInviteClose() {
window.location.reload(true);
}
Note: All the code is place in the calendar.js file which has references in both parent and radwindow page.
Have a read of this What causes the error "Can't execute code from a freed script"
The try / catch solution may help in your case.
You can hook the OnClientColose to the RadWindow and refresh the page using document.location.reload().
function RefreshParentPage()//function in parent page
{
document.location.reload();
}
Thanks,
Saritha
Make sure the code is executed in the correct context, i.e., in the main page and not in the CalendarInvites page.
Then, if this is ok, try adding a timeout like this:
function onSendInviteClose() {
setTimeout(function() {
window.location.reload(true);
}, 0); //you can also try increasing the timeout.
}
Usually, the cannot execute code from freed script means that the content page that is being disposed is still trying to run some code but it is orphaned (e.g., other pieces of code it depends on are disposed, or its context is gone - the window object). Most often this would be the page in the iframe (i.e., inside the RadWindow), but it may also be your main page.
Attached the 'onSendInviteClose' method on close event.
var win = $window.createPopup(url,
{
size: "750, 500", behaviors: Telerik.Web.UI.WindowBehaviors.Close |Telerik.Web.UI.WindowBehaviors.Move, onclose: onSendInviteClose
}
Now create that method in the page from which the popup is opened. This function will automatically fired when popup window is closed.
function onSendInviteClose() {
//get a reference to the current RadWindow
var wndow = GetRadWindow();
wndow .Close();
// and as this method is present in parent page
// we can refresh the page controls easily.
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}

Checking for unsaved changes when switching views in backbone.js

I am new to Javascript and backbone.js, so hopefully I am missing something simple here. I am experimenting with some sample code I found which is supposed to check for unsaved changes before allowing a user to navigate away to another page. I have created a JSfiddle here:
http://jsfiddle.net/U43T5/4/
The code subscribes to the hashchange event like this:
$(window).on("hashchange", router.hashChange);
And the router.hashChange function checks a "dirty" flag to determine whether or not to allow the navigation, like this:
hashChange: function (evt) {
if (this.cancelNavigate) { // cancel out if just reverting the URL
evt.stopImmediatePropagation();
this.cancelNavigate = false;
return;
}
if (this.view && this.view.dirty) {
var dialog = confirm("You have unsaved changes. To stay on the page, press cancel. To discard changes and leave the page, press OK");
if (dialog == true) return;
else {
evt.stopImmediatePropagation();
this.cancelNavigate = true;
window.location.href = evt.originalEvent.oldURL;
}
}
},
The problem is that the code is not working because this.view is undefined, so the 2nd if block is never entered.
I would like the sample program to always ask for confirmation before navigating away from the page (in my sample program, I have set this.view.dirty to always be true, which is why it should always ask for confirmation). Or if there is a better approach, I am open to alternatives.
The main issue is the this context in the methods , this corresponds to the Window Object and not the Router. So it always remains undefined as you are defining view on the router. Declare a initialize method which binds the context inside the methods to router.
initialize: function() {
_.bindAll(this, 'loadView', 'hashChange');
},
Check Fiddle
I spent a lot of time to make at least something decent.
I ended up writing a wrapper for Backbone function:
var ignore = false;
Backbone.history.checkUrl = function() {
if (ignore) {
ignore = false;
return;
}
app.dirtyModelHandler.confirm(this, function () {
Backbone.History.prototype.checkUrl.call(Backbone.history);
},
function() {
ignore = true;
window.history.forward();
});
};
app.dirtyModelHandler.confirm is a function which shows confirmation (Ok, Cancel) view and takes success and cancel functions as arguments.

Controlling browser window through Javascript

I have a web app that launches an URL in other windows/tabs. I would like to check if the window/tab exists. If not, I want to create it, else I would like to pick it in the first position.
I use:
wf=window.open(address, web_form_target, 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=640,height=450');
if(wf!=null)
wf.focus();
But it goes well only the first time (in IE, not in Firefox). If I create a new tab in the window, when I call window.open() nothing happens. If I close the window it recreates it but keeps it ionized.
Is there a way I can follow to obtain a good result?
Thanks in advance.
Here's some code I've used for ages that still works as far as I know. Notice that oWindow has global scope, and that I pass it to the second parameter of open as a string, not as the object itself. Then, I test to see if it's closed before trying to open again...if it's already opened, then I just give it focus:
var oWindow;
function openWindow(p_strURL) {
if(!oWindow || oWindow.closed) {
oWindow = window.open(p_strURL, "oWindow", "status, scrollbars, resizable, width=800, height=500");
if(!oWindow.opener) {
oWindow.opener = window;
}
}
else {
oWindow.location.href = p_strURL;
oWindow.focus();
}
}
Hope it helps you find a solution,
Kevin
web_form_target is the window name.
if (wf.name !== web_form_target) {
// create it
}

Categories

Resources