Hardware Back button closes the app

Hardware back button closes the app after loading,

function loadTemplate() {
if (loadPageTemplateDeferral) {
return loadPageTemplateDeferral.promise;
}

  loadPageTemplateDeferral = $q.defer();

  $ionicTemplateLoader
    .load('report/show_report/show_report.html')
    .then(function (template) {
      pageTemplate = template;
      loadPageTemplateDeferral.resolve(template);
    });

  return loadPageTemplateDeferral.promise;
}

}
}

Please help

Dont understand your question.
If you are are on the initial state, hardware back button will take you out of the app unless it is handled using $ionicPlatform.registerBackButtonAction().

where to handle the register back button? I mean the location.

In the controller of the page you want it to be handled

I have one more issue when I click for the hardware back button first time the app exits., when I click the in-app back button first and then hardware back button it works perfect. How to sort this?

By inapp button, if you mean ion-nav-back-button, then it fires $ionicHistory.goBack() method Documentation, where it will the check the view stack in the history and go to the last visited view. Maybe your inapp button is taking to a null pointer and does nothing.

Secondly, If the page is the 1st page, then it shouldnt have a back button on the nav-bar unless it is for sidemenu (in my opinion)

Thirdly,You have to post some code and explain a bit more clearly what exactly is the issue

Exactly, It does the same. it fires the ionic history. How can I go back directly to main page?

First, you have to specify the flow of your app(Main page->next page etc) and where you are facing the issue. Its difficult to know where exactly is the problem. Is it the main page or any next page in the nav flow

I have 2 pages. From Mapin Page, click the second page. It enters the 2nd page

The issue happened here in the second page it doesn’t go to the main page when I click hardware back button. It exits.
If I click the in-app back button it goes to the main page
Again if I come to the second page and if I click the hardware back button it works perfectly.

strange.
are you deleting your ionicHistory?
are you changing the root view while navigation?

Also, check the console log for any errors

No, I am not deleting the ionicHistory
I am not changing the root view
No errors in error log

Well you could handle the registerBackButtonaction event but something doesn’t seem right.
Can you create a blank project and do a simple nav from page1 to page 2 and see if it works and if it doesnt, then share the code of this project

Secondly, how are you navigating between page1 to page2

You can also check for history stack as described here:

Fixed the hardware back button… Thanks @thesourav

Good for u…How DD u do it

Just adding forcerefresh function === true