I am trying to set a different root page on pressing the hardware back button on a specific page, I am using this method this.platform.registerBackButtonAction
to change the default action
Here is my use case:
This (image below) is MyProfile page (not a root page), where a user can toggle between 2 profiles (Free mode and Restricted Mode) and the user can also check Personal details (this navigates to a different page). The scenario here is when a user chooses any of the modes, and when the user goes back I want the user to land up in respective root pages (FreeMode and RestrictedMode). Now I have managed to set the root page when the user taps the back button on the NavBar.
This is my ts code.
import { Component, ViewChild } from '@angular/core';
import { NavController,Platform, Navbar , List} from 'ionic-angular';
import { ToastController } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage';
import { FreeModePage } from '../free-mode/free-mode';
import { RestrictiedModePage } from '../restricted-mode/restricted-mode';
@Component({
selector: 'page-profile',
templateUrl: 'profile.html'
})
export class ProfilePage {
//@ViewChild(Content) content: Content;
@ViewChild(Navbar) navBar: Navbar;
@ViewChild(List) list: List;
public unregisterBackButtonAction: any;
constructor(public navCtrl: NavController, private nativeStorage : NativeStorage, private toastCtrl : ToastController, private platform : Platform,
) {
}
ionViewDidLoad()
{
this.navBar.backButtonClick = (e:UIEvent)=>{
if(this.selectedProfile == 'FreeMode'){
this.nativeStorage.setItem("userProfile", { userProfileType: this.selectedProfile });
this.navCtrl.setRoot(FreeModePage)
} if(this.selectedProfile == 'RestrictedMode'){
this.nativeStorage.setItem("userProfile", { userProfileType: this.selectedProfile });
this.navCtrl.setRoot(RestrictiedModePage);
}
return true;
// this.navCtrl.pop();
}
this.customHardwareBackButton()
// this.scrollBottom();
}
ionViewCanLeave() {
//this.unregisterBackButtonAction && this.unregisterBackButtonAction();
}
customHardwareBackButton():void
{
this.platform.registerBackButtonAction(function(event){
if(this.selectedProfile == 'FreeMode'){
this.nativeStorage.setItem("userProfile", { userProfileType: this.selectedProfile });
this.navCtrl.setRoot(FreeModePage)
} if(this.selectedProfile == 'RestrictedMode'){
this.nativeStorage.setItem("userProfile", { userProfileType: this.selectedProfile });
this.navCtrl.setRoot(RestrictiedModePage);
}
// this.navCtrl.pop();
}, 1);
}
}
Now the real issue is, my hardware back button’s default action is blocked, but it doesn’t navigate and set the rootpage. That’s where I am stuck.
I also cannot use ionViewCanLeave() method as it will trigger and navigate to rootpage when I tap PersonalDetails link. Please help me out on this one.
https://i.stack.imgur.com/ApKeD.png