[v2] where to write android back button code

Hi,

I tried a lot but unable to implement back button code in v2.

When user is on ‘index.html’ or ‘dashboard.html’ page. If user press on back button, he should exit from app with confirmation dialog otherwise navigate to previous page.

I have phonegap code for back button but unable to understand where and how to implement this.

Please help. I really need this.

Previously I was able to do in v1 with below code:

document.addEventListener("deviceready", appReady, false);
 function appReady(){
 	document.addEventListener("backbutton", function(e){
 		var page = getCurrentView(app);
 		app.dialog.alert(page);
 		if(page.name=="index"){
 			navigator.notification.confirm("Are you sure want to exit?", onConfirmExit, "My Project", "Yes,No");
 		}
 		else{
 			navigator.app.backHistory();
 		}
 }
 function onConfirmExit(button){
 	if(button==1){
 		navigator.app.exitApp();
 	}
 	else{
 		return;
 	}
 }

Hey @Md_Danish

What is the problem?

You need to know what page the user is on?

Regards

@DanielRiera Exactly!

Hey @Md_Danish

You can use this

var currentPage = $(".page.page-current").attr("data-page")

or the attribute that you need.

Hi did you already could use backbutton? And read current page? Im with the same problem

v2 has deviceready event listener in.
So all addition listeners are installed in global init hook

Example

// Init App
var app = new Framework7({
  id: 'io.framework7.testapp',
  root: '#app',
  theme: theme,
  data: function () {
    return {
      user: {
        firstName: 'John',
        lastName: 'Doe',
      },
    };
  },
  methods: {
    helloWorld: function () {
      app.dialog.alert('Hello World!');
    },
  },
  routes: routes,
  vi: {
    placementId: 'pltd4o7ibb9rc653x14',
  },
  on:{
      init: function () {
          console.log("App Init");
          //set corodova listener here
        }
  }
});