简体   繁体   中英

How to create a back button to previous page on Android with Titanium Studio?

I have created a back button to take me to the previous page. See code bellow:

var backbutton = Titanium.UI.createButton({
    title:'back',
    bottom: 10,
    left: 10,
    zIndex:2
});
win3.add(backbutton);

I add a addEventListener to backbutton. See code bellow:

backbutton.addEventListener('click',function() {
    var win = Titanium.UI.createWindow({
        url:'alarmgroups.js',
        title:'Sensor/Larm Objekt'
    });
    win.open({modal:true});
    win3.close();
    win3.hide();
});

Know I wonder what the problem could be. When Im using the code above It makes the Application crash. Im using zIndex on every .js page that I have in my project, but I dont know if Its right to do so. I use win.open({modal:true}); and after that code I run win3.close(); and win3.hide();. win3 Its my current window.

Does anyone having a solution on how to create a back button for Android?

You have two native solutions to create a back button on android, the first one is adding a back button to the action bar:

To achieve this, you have to edit the android's action bar in the window's open event. (Note: do not use modal:true while opening the window)

var window = Ti.UI.createWindow({
   title: "test",
   backgroundColor: "white",
});

window.addEventListener('open', function({
   window.activity.actionBar.onHomeIconItemSelected = function() { window.close(); };
   window.activity.actionBar.displayHomeAsUp = true;
});
window.open();

The second way, is overriding the android's back button of the current window.

var window = Ti.UI.createWindow({
   title: "test",
   backgroundColor: "white",
});

window.addEventListener('androidback', function({
    window.close();
});
window.open();

Try this :

var win = Ti.UI.createWindow({
title:'Hello world',
backgroundColor:'#fff',
fullscreen:false
});
   win.addEventListener('androidback',function() {
// do something
});

Also,here is the link : Android Back Button in Titanium.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM