Initially, I am in view "A" and then i loaded a few things from api. Now when on button click, I move to View "B" and then when back button click (hardware/nav bar back button), I moved back to View A, but view A's content was already destroyed and only simple view got loaded.
window.history.back();
So, I came with a way, i.e. on every view load, I added it in my localstorage state array. and when back pressed, I routed back to previous state.
this.router.navigate([previousState]);
But this works only when my nav bar button click. On hardward button click, i get navigated back but my state is not fetched again.
My code is:
@Injectable()
export class AppStateService {
private menuOpen: boolean = false;
private headerTitle: string = "";
private offlineState: boolean = false;
public constructor(private router: Router) {
document.addEventListener("backbutton", () => {
this.back(true);
}, false);
}
public back(hardwareClick?: boolean): void {
var routes: string[] = [
'Login',
'Main'
];
for (var i of routes) {
var route = this.router.generate([i]);
if (this.router.isRouteActive(route)) {
this.exitApp();
return;
}
}
var previousState = CordovaService.getPreviousState();
this.router.navigate([previousState]);
}
Visual example:
though from B to A works fine when nav bar back button click What might be the problem?



You are trying to solve a problem that was created by yourself alone. You should not create multiple pages on the same Cordova app to begin with.
This is the first of the Best Practices Cordova app development principles:
Yes, you might need to write more code, but you may still use multiple JS files to create your views in the single page. The multiple pages approach also features performance penalties: