How to exit application using tizenhwkey event.

In tizen wearable device, application can bind 'tizenhwkey' event from Bezel Swiping Down gesture.

When application bind 'tizenhwkey' event, it checks page id and decide go back/exit with several lines of app-side script.
Tizen Device api provide application exit method. To get more information, please refer here.
Even if the application has many pages, it can handle page back/app exit process.

In this page, we can show some example case with main(first) page.

Basic page

When Main(first) page does not have any popup or drawer, it is so simple to handle 'back' event. Just remember id of main page.
In this page, let's consider the id of main page as "main".

To add a page back/app exit logic to the application, use the follosing code:

( function () {
    window.addEventListener( 'tizenhwkey', function( ev ) {
	if( ev.keyName === "back" ) {
		var page = document.getElementsByClassName( 'ui-page-active' )[0],
			pageid = page ? page.id : "";
		    if( pageid === "main" ) {
			try {
				tizen.application.getCurrentApplication().exit();
			} catch (ignore) {
		        }
		    } else {
			window.history.back();
		    }
	}
    } );
} () );

Main page with Popup

If the Main page has active(opened) popup, it also needs to check popup.
To add a page back/app exit logic to the application, use the follosing code:

( function () {
    window.addEventListener( 'tizenhwkey', function( ev ) {
        if( ev.keyName === "back" ) {
            var activePopup = document.querySelector( '.ui-popup-active' ),
                page = document.getElementsByClassName( 'ui-page-active' )[0],
                pageid = page ? page.id : "";

            if( pageid === "main" && !activePopup ) {
                try {
                    tizen.application.getCurrentApplication().exit();
                } catch (ignore) {
                }
            } else {
                window.history.back();
            }
        }
    } );
} () );

Main page with Drawer

If the Main page has opened drawer, it also needs to check drawer.
To add a page back/app exit logic to the application, use the follosing code:

( function () {
    window.addEventListener( 'tizenhwkey', function( ev ) {
	if( ev.keyName === "back" ) {
	    var page = document.getElementsByClassName( 'ui-page-active' )[0],
		openDrawer = page.querySelector('.ui-drawer-open'),
		pageid = page ? page.id : "";
	    if( !openDrawer && (pageid === "main") ) {
		try {
		    tizen.application.getCurrentApplication().exit();
		} catch (ignore) {
		}
	    } else {
		window.history.back();
            }
	}
    } );
} () );

Where to Go Next