Back button with hardware and software act different

The existing app is created with side menu template.
My structure is

Homepage–>page 1–>page 2 --> page 3

from page 2, i go to page 3, then back to page 2, go to page 3 again.

In order to back to homepage,

with software back button (left top header)
from page 3, press back to page 2
then, press back to page 1
then press back to homepage

with android hardware back button
from page 3, press back to page 2
then, press back to page 3
then press back to page 2
then press back to page 1
then press back to homepage.

Why there is a difference between software back button with hardware?
Obviously the hardware button not working correctly. How to make both work the same?

Keep same code in both back button
I think your flow is page3 > page 2 > page 1 > Home page
Then use

$window.history.go(-1);
It is working for both of that…I’m using this