Hi I have implemented a side bar navigation. It has 3 options like a sidebar menu in that first screen(i.e index.js) is the main screen. The problem is when I go to any screen via sidebar menu then again comeback to main screen and then from main screen if I press back button it goes to last screen visited.The expected behaviour should be that if I come back to main screen it should exit application. So how to track the main screen. And if I am on the main screen it should anyhow exit application rather than going from main screen to last visited screen.
Here is the code. The code is bit lengthy. Can anyone help on this?
index.xml file
<Alloy>
<Window id = "win" onOpen="openCurrentIssue">
<View id="view1" width="75%" height="Ti.UI.FILL" left="0%" >
<TableView id="menuTable"></TableView>
</View>
<View id="view2" width="Ti.UI.FILL" height="Ti.UI.FILL" backgroundColor="#A9F5A9" >
<View id="viewcheck1" >
<Label id="title" width="Ti.UI.SIZE" text="PolymerCommunique" height="Ti.UI.SIZE" textAlign="Ti.UI.TEXT_ALIGNMENT_CENTER"></Label>
<ImageView id="menuImg" image="/images/menu.png" onClick="showsideBar" left="5"></ImageView>
</View>
<View id="viewcheck2" width="Ti.UI.SIZE" height="Ti.UI.SIZE" backgroundColor="#A9F5A9" layout="vertical">
<Label id="cIssue" text="Demo" width="Ti.UI.SIZE" height="Ti.UI.SIZE" textAlign="Ti.UI.TEXT_ALIGNMENT_CENTER" top="10" color="black"></Label>
<ImageView id="cImage" width="Ti.UI.SIZE" height="Ti.UI.SIZE" top="45"></ImageView>
<Button id="cButton" title="Advertiser"></Button>
</View>
<View id="viewBelow" width="150" height="Ti.UI.FILL" backgroundColor="#A9A5A9" left="-150" visible="false" top="40">
<TableView id="menuTable"></TableView>
</View>
</View>
</Window>
index.js file
var isMenu = false;
function showsideBar(e) {
try {
if (isMenu) {
$.viewBelow.animate({
left : -150,
//left :0,
duration : 300,
curve : Ti.UI.ANIMATION_CURVE_EASE_IN_OUT
});
isMenu = false;
} else {
$.viewBelow.visible=true;
$.viewBelow.animate({
left : 0,
duration : 300,
curve : Ti.UI.ANIMATION_CURVE_EASE_IN_OUT
});
isMenu = true;
}
} catch(e) {
Ti.API.info('Exception from index.js ' + e.value);
}}
function openCurrentIssue(e) {
try {
var url = "http://polymerscommunique.com/api/current_issue.aspx";
var jsonResponse;
var response;
var xhr = Ti.Network.createHTTPClient({
onload : function() {
Ti.API.info("Received text: " + this.responseText);
jsonResponse = JSON.parse(this.responseText);
$.cImage.image = jsonResponse[0].cover_image_url;
$.cIssue.text = jsonResponse[0].issue_title;
},
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000
});
xhr.open("GET", url);
xhr.send();
} catch(e) {
Ti.API.info('Exception from index.js ' + e.value);
}}
createTableRow = function(args) {
var row = Ti.UI.createTableViewRow();
var parentView = Ti.UI.createView({
width : Ti.UI.FILL,
height : 40,
left : 5
});
var childView = Ti.UI.createView({
height : Ti.UI.SIZE,
width : Ti.UI.FILL,
layout : "horizontal"
});
var image = Ti.UI.createImageView({
image : args.leftImage,
width : 18,
height : 20,
left : 5
});
childView.add(image);
var title = Ti.UI.createLabel({
color : "white",
text : args.title,
left : 10,
font : {
fontSize : 17,
fontWeight : 'bold'
}
});
childView.add(title);
parentView.add(childView);
row.add(parentView);
var separator = Ti.UI.createView({
bottom : 0,
height : "1dp",
width : Ti.UI.FILL,
backgroundColor : "#303030"
});
row.add(separator);
return row;};
var rows = [];
rows.push(createTableRow({
title : 'Current Issue',
leftImage : '/home.png'}));
rows.push(createTableRow({
title : 'Past Issues',
leftImage : '/settings.png'}));
rows.push(createTableRow({
title : 'Subscribe',
leftImage : '/logout.png'}));
$.menuTable.setData(rows);
$.menuTable.addEventListener('click', function(e) {
if(e.index=="0"){
Alloy.createController('index', 'just');
}
if (e.index == "1") {
showsideBar();
Alloy.createController('pastissues', 'just');
}
else if (e.index == "2") {
showsideBar();
Alloy.createController('check','just');
} });
$.win.open();
There are two other files also as seen from in table listener. If that is also needed please let me know.
You can listen to the back button event in your main window controller, then end the current activity on click:
$.home.addEventListener('android:back', function (e) {
var activity = Titanium.Android.currentActivity;
activity.finish();
});