NavController cache problem


#1

Hi everyone,
I’m going to disable native back button in Android and implement its function using nav.pop.
By the way NavController inject instance is cached on the page.
What is the best way to achieve my goal?
Sorry for my bad english.


#2

Here is my code for better understanding.

export class MyApp {
@ViewChild(Nav) nav;
constructor(
   ...
   platform: Platform
   ...
   ) {
        platform.registerBackButtonAction(()=>{
            alert(this.navCtrl.getViews().length);
            this.navCtrl.pop();
        });
   }
   ...
}

The length of views is 1 always even though there are many views.


#3

I suspect you have only one view in the root nav stack, which is what you’re interacting with. Why not just let the navigation system do its job instead of trying to micromanage it?


#4

Excuse me, would you like to explain in detail?
The app is closed when back button is touched in Android system.
So I want to disable it.


#5

Shows us please, .length says otherwise.


#6

I was able to solve problem by using app.getActiveNav() function.


#7

Could you share the complete solutions for future readers? Thanks.


#8

My solution is like following:

constructor(
        public navCtrl: NavController,
        public settings: SettingsService,
        public appCtrl: App,
        platform: Platform,
    ) {
         platform.registerBackButtonAction(()=>{
            if (this.appCtrl.getActiveNav().getViews().length > 1) {
                this.appCtrl.getActiveNav().pop();
            }
        });
}