How force Ionic 4 back button to always goto a certain Page from selected pages

I am porting my Ionic 3 app to Ionic 4. I have switched over to using the new routing, with lazy loaded modules etc.

I have a side menu where I can select my main pages from any other page.

However, whenever I click the back button from one of the main feature pages, I always want to go back to the main home page (and not back to perhaps another feature page in the current route stack)

The click handler code in the Ionic 4 version for the side menu selection is as follows (pageItem.pageRoutePath contains the new route)

      public openPage(pageItem: AppPageItem): void {
		try {
		  this.logger.debug(`openPage ${pageItem.title}`);
		  
          // Try to force homepage to be where the back button takes us (but this doesn't seem to work)
		  this.navController.navigateRoot(Constants.vals.pageRoutePaths.home, { replaceUrl: true });
		  if (pageItem.pageRoutePath != Constants.vals.pageRoutePaths.home) {
			this.navController.navigateForward(pageItem.pageRoutePath);
		  }
		} catch (error) {
		  this.logger.error(`openPage ${error}`);
		}
	  }

The { replaceUrl: true } parameter was me experimenting trying to get this to work, but it did not make any difference.

In Ionic 3 I could do what I am describing as follows…

this.nav.setRoot(HomePage);
  if (pageItem.page != HomePage) {
    this.nav.push(pageItem.page); 
  }  

I don’t want this from every page, just my feature pages, eg some of the feature pages navigate to "details: pages, and here I do want the back button to just return back to the page it was navigated from, ie the default behavior. Also, I do have one feature page where I do open it as a details fro another feature page, so in this case i do want the detail behaviour of the back button.

So, I just really want to switch the behaviour when the page is opened from this side menu.

Does anyone know how I can achieve this in Ionic 4?

Thanks in advance for any help

Got it working.

All I need to do was use the replaceUrl on the new route…

this.navController.navigateForward(pageItem.pageRoutePath, { replaceUrl: true });

I think also there was an issue with this working with Ionic < 4.2 (it works with Ionic 4.3 which I just upgraded to)

well good for you cheers :slight_smile:

An update, I have just noticed have’t quite got this right yet. The replace replaceUrl works if I on;y have the last url I want to replace, but if I have a stack of more than two pages, the back button now will not bring me back to my home page.

So, my question is only partly solved, iut still does not quite do what I was after