Ionic 2 App Re-Display page on start-up

Does anyone know how I could either a) force my app to re-start from scratch every time the user launches the app or b) re-display a particular page when the app re-starts e.g on resume.

I have a tabbed Ionic 2 application with a login page which I display on start-up, and then when the user logs in my TabsPage is pushed onto the nav. I want to get that initial login page to display every time the user opens the app, rather than have the application open up with the tabs already open.

In my app.js I have:

class MyApp {
constructor(platform: Platform) {
this.root = LoginCtrl;

platform.ready().then(() => {
  this.bindEvents();
});

}

bindEvents: function () {
document.addEventListener(ā€˜resumeā€™, this.onResume, false);
}

onResume: function () {
console.log(ā€œon resumeā€);
this.root = LoginCtrl;
}

}

This doesnā€™t work i.e the LoginPage is not re-displayed although the onResume event is firing.

What would I need to do to get that to work?

2 Likes

Your app.html should have something like

<ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav>

then if you set rootPage to something useful in your app.js, thatā€™s the first place the app will go when you initialize it.

this.rootPage = LoginPage;

Does that work for you?

@richardshergold Did you figure out how to do this? I need to do this as well.

I canā€™t remember to be honest. I think possibly not.

This worked for me:

      // Listen for the app coming from the background into the foreground
      document.addEventListener('resume', () => {
        setTimeout(() => {
          this.onResume();
        });
      });
2 Likes

You can also use the Platform API form Ionic: http://ionicframework.com/docs/v2/api/platform/Platform/

this.platform.resume.subscribe(e => {
    this.onResume();
});
9 Likes