Skip to content

Ionic 4 <ion-back-button> issue #17421

Closed
Closed

Description

@CitizenOfEarth

Ionic version:

[x] 4.10.2

I'm submitting a ...

[x] bug report
[ ] feature request

Current behavior:
When I navigate back with an ion-back-button on a tabs template I always end up on the root page.
How do I solve this?

Expected behavior:
I expect it to navigate back to the site I was before and NOT to the root page.

Steps to reproduce:
$ ionic start
Select the tabs template
$ ionic generate page [your page name here]
Add a ion-button to tab2
Add: (click)="go()" to the button.
Add: constructor(public router: Router){}
Add: import { Router } from '@angular/router';
Define go() in the TypeScript file like this:
go() {
this.router.navigate(['yourpagename'])
}
On the generated Page add in the toolbar an ion-back-button
$ ionic serve -l

Related code:

tab2.page.html:

<ion-button (click)="go()">Page 2</ion-button>
__________________________________________________________
tab2.page.ts:

import { Router } from '@angular/router';

constructor(private router:Router){}

go(){
  this.router.navigate(['page']);
}
__________________________________________________________

page.page.html:

<ion-header>
  <ion-toolbar>
    <ion-back-button></ion-back-button>
    <ion-title>Page</ion-title>
  </ion-toolbar>
</ion-header>



Other information:
The src folder of my App:
src.zip

Ionic info:

Ionic:

   ionic (Ionic CLI) : 4.10.2

System:

   NodeJS : v10.15.0
   npm    : 6.4.1
   OS     : Windows 10

Activity

  1. 3alampro commented on Feb 8, 2019

    @3alampro

    I'm having the same issues occur only with tabs pages but if I navigate from regular page into any page and hit the back button it work as expected the same page if I navigated from tab page and hit the back button i end up in the root page

  2. Caturix2199 commented on Feb 8, 2019

    @Caturix2199

    Same issue here. Also tried to update angular/router. Didn't change anything.

  3. sukheja-varun commented on Feb 8, 2019

    @sukheja-varun

    Can you try this

    <ion-buttons slot="start">
        <ion-back-button  defaultHref="/"></ion-back-button>
    </ion-buttons>
    
  4. CitizenOfEarth commented on Feb 9, 2019

    @CitizenOfEarth
    Author

    @sukheja-varun it doesn‘t work
    At defaultHref I tried many things:
    /
    /tab2
    tab2
    /tabs/tab2
    tabs/tab2

    Nothing works

  5. sukheja-varun commented on Feb 9, 2019

    @sukheja-varun

    Can you please check which ionic version is installed.
    Use below command on your project terminal
    npm list @ionic/angular

  6. kyleabens commented on Feb 9, 2019

    @kyleabens

    defaultHref only comes into play when you navigate directly to that page because it doesn't know the stack.

    I'm also having this issue but I'm using ion-segment that changes based on url which I suppose is the same thing as tabs. I have not found a solution yet.

  7. thenaim commented on Feb 9, 2019

    @thenaim

    same issue #15216 ?

  8. CitizenOfEarth commented on Feb 12, 2019

    @CitizenOfEarth
    Author

    @sukheja-varun I did the command and this was the output:

    -- @ionic/[email protected]

  9. 442623641 commented on Feb 27, 2019

    @442623641

    same issue,I resolve this issue with routerDirection , eg:<ion-button routerLink="/path/to/page" routerDirection="back"><ion-icon name="arrow-back"></ion-icon></ion-button>

  10. kensodemann commented on Feb 27, 2019

    @kensodemann
    Contributor

    @442623641 - I have been being intentional about where I go back to, like this:

        <ion-buttons slot="start">
          <!-- <ion-back-button defaultHref="tabs/customers"></ion-back-button> -->
          <!-- see https://github.com/ionic-team/ionic/issues/15216 -->
          <ion-back-button
            defaultHref="tabs/customers"
            (click)="navController.navigateBack('tabs/customers')"
          ></ion-back-button>
        </ion-buttons>
    

    Obviously, if the page is accessible from more than one parent, I would need to be a bit smarter about that, but doing so is a relatively trivial exercise.

  11. servrox commented on Mar 4, 2019

    @servrox

    @kensodemann When you navigating back like this, you will lose the state inside your tabs. This is because internally it navigates back to /tabs and then to the route specified in your (click) function.

    I also created a custom ionic component to cover the relatively trivial exercise.

    See my solutions to navigate back without losing tab-states.

  12. zxdeer commented on Mar 8, 2019

    @zxdeer

    same issue,I resolve this issue with routerDirection , eg:<ion-button routerLink="/path/to/page" routerDirection="back"><ion-icon name="arrow-back"></ion-icon></ion-button>

    Child page repeatedly jump still can not return to the main page

  13. wkande commented on Mar 21, 2019

    @wkande

    Works with or without routerDirection="back" not sure why or if it is needed

    <ion-back-button defaultHref=""  routerLink="/tabs/tab3"  routerDirection="back"></ion-back-button>
    
  14. Eraldo commented on Apr 10, 2019

    @Eraldo

    If I am coming from another page e.g. SomePage and then pushing a tabbed page "onto the stack" this.navController.navigateForward('tabs/tab1') and then click on the back button, I would expect to get back to SomePage however that is not what is happening at the moment. Instead I land at the tabs page instead of at the page where I came from.

    If I use the browsers back button I do in fact get the desired behaviour (getting back to SomePage).

    I guess this is because "back" first sends me back to the tabs page (on step back).
    So the issue might be that navigating to tabs/tab1 is actually creating 2 pushes onto the "back stack" instead of one?

    I don't fully get it.

    Sadly neither of the solutions above worked for me yet. (or I did something wrong)

    Insights are welcome. ;)

  15. wkande commented on Apr 10, 2019

    @wkande

    Here is another idea using angular/common.

    import { Location } from '@angular/common';
    
    
    constructor(private router:ActivatedRoute, private modalController:ModalController,
        private events:Events, private _location:Location) { 
     }
    
    myGoBackFunction(){
      this._location.back();
    }
    
  16. liamdebeasi commented on Apr 10, 2019

    @liamdebeasi
    Contributor

    Hi there,

    This issue has been resolved, and I have published a nightly build of Ionic with the fix. You can install it with npm i @ionic/angular@dev.

    Please let me know if you are still running into any issues with this.

    Thanks!

  17. ionitron-bot commented on May 10, 2019

    @ionitron-bot

    Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

  18. locked and limited conversation to collaborators on May 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions