[Feature Request] Back navigation with parameters
Short description of the problem:
It would be useful to be able to pass parameters back in the navigation. Currently, we can use this.nav.push(NextPage,NavParams) to send information to the NextPage, but no way to receive information back when NextPage is closed.
What behavior are you expecting?
Would be nice to have something like this.pop(NavParams) where the NavParams could be delivered to onPageWillEnter(NavParams) method in the parent Page.
Ionic Version: 2.x
Browser & Operating System: iOS / Android / Chrome
Right now I am getting around the issue by pushing the parent as a NavParam to the child, so
nav.push(ChildPage, {delegate: this});
Then when it is time to pop the child page off, I have pass the data to the delegate before hand. This method works okay but it couples your views together.
I think this is a good idea, just not sure on the best API for this yet. There's a few ways we can pull this off, but I want to make sure it's easy enough to understand and use without causing more issues. When going forward, push(Page, data) is pretty easy because that page knows exactly what should happen, and was passed the exact data to load. However, when it is the entering page from a pop(), the entering page isn't sure how it came about being the active page again, and the constructor wouldn't fire again since it's still instantiated.
I like the idea of passing data to onPageWillEnter(leavingPageData) and onPageDidEnter(leavingPageData). It's pretty generic and similar to how the constructor NavParams work. All pages themselves should have no clue where they live in the nav stack, and what pages come before and after them.
So the question is, what's the easiest way to wire up data getting passed from the leaving page, to the entering page? Because both the leaving page and entering page require some sort of API, and preferably something familiar to how data is passed around in Ionic2 and ng2.
One idea is to have the leaving page's onPageWillLeave() return some data, which then gets passed as the argument in the entering page's onPageWillEnter(leavingPageData), but I'm just not sure about that API yet.
Any other thoughts?
For keeping it clean, maybe use the same system has modal with a "dismiss" like function with you can put data in there and subscribe to a callback :
MyFirstPage :
goToMyPage() { nav.push(MyPage); MyPage.onPop(data => { console.log(data); }); }
MyPage :
dismiss() { this.viewController.dismiss(data); }
The modal Page is wrapped in a Modal instance, the way we open a new Page today is by passing the class to nav.push(), so we would need to create an wrapper to push pages too.
I am not an IOS developer, but the way Android framework works is by setting an REQUEST_ID when opening the child Page and returning it to the callback method in parent Page when the child pops out. So the parent page can validate from where come pop call. It helps when the parent has more than one possible child.
Something to keep in mind too is that, onPageWillEnter is triggered when swapping from a tab to another.
@adamdbradley, is there any chance of this to be included in the betas for v2? This is something I really need.
Any solution to this (pop and pass nav params) yet?
Also looking for an easy solution for this. Have a settings page and want to refresh the original view when navigating back after settings have been changed. Some sort of hook that I can put in onPageWillLeave to pass data back to the previous view. I guess potentially:
onPageWillLeave(nextViewParams) {
// modify the next/back view params here
nextViewParams.settingsChanged = this.dirty;
}
Also looking into the same functionallity, as it could mimic the look, behaviour and feel of the native iOs select boxes.
@adamdbradley Passing the data in the pop is easy and familiar because you're using it for .push() just the same. Retrieving the data in the parent inside onPageWillEnter() is a good option.
I guess a developer knows what he's doing when he'll pop some data to another page. In the rare occasion this data is uncaught, I guess it's not going to hurt?
I have this need as well. I've been digging into NavController and ViewController, but so many methods are private that any use of their internals would be a tenuous hack.
You can get data back by keeping a reference to an object provided to NavParams:
Parent:
this.nav.push(Child, {container: this.myDataContainer});
Child:
this.navParams.get('container').foo = 42;
Idea: this.push could return a Promise. This indicates a clear connection with the specific ChildPage and its arguments and the resolution code in the Parent.
this.push(ChildPage).then(dataFromChild => {
// do stuff with data resolved from Child
});
Then either put the Promise as a parameter of the Child's enter methods onPageDidEnter(promise)
Or maybe inject it?
constructor(parentPromise: ParentPromise)
However, for this to work, the Parent instance cannot be unloaded. Does Ionic2 promise to not unload Pages in the Nav stack?
The second issue is that developers may assume that the Parent Page's resolution code (function in .then) is executed when the Page is active. The documentation could highlight this issue and Developers could code around this by saving the data and then retrieving it in onPageDidEnter() or just saving the Promise and calling .then in on enter methods.
this._childPromise = this.nav.push(ChildPage);
onPageDidEnter() {
if (popFromChildPage) {
this._childPromise.then(data => {
// do stuff
}
}
}
But at the moment, the code to determine if nav popped from a particular ChildPage is a bit awkward. And now there's this disconnection between the push and the resolution code.
So maybe ParentPromise could be a special Promise that does not call .then immediately when resolved. Instead, it waits for the Parent Page to be active again, and then if resolved/rejected, invokes then/error.
Also, one might argue that you want an Observable instead, or to control whether to use the active view Promise behavior. So maybe something like this:
this.nav.push(ChildPage).observe().subscribe(data => {
});
this.nav.push(ChildPage).observe({whenPageActive: true}).subscribe();
this.nav.push(ChildPage).promise().then(data => {
});
this.nav.push(ChildPage).promise({whenPageActive:true}).then();
What do people think?
Now that I think about it more, I don't think the whenPageActive concept is a good idea. What if there's a navigation from the ChildPage to a completely different part of the app? Do whenPageActive Promises only fire when the nav stack is preserved? i.e. A pop() from Child. It just raises more confusion and makes this feature more complicated than needed.
Better to just remind developers that the Promise/Observable will be getting data while the Page is not active and to design their code appropriately.
At the moment, you can pass a Promise through NavParams with code like this:
Parent
new Promise((resolve, reject) => {
this.nav.push(ChildPage, {resolve: resolve});
}).then(data => {
// process data
});
Child
this.navParams.get('resolve')('some data');
The Promise creation code is just a bit awkward.
+1 on this? Should we expect this feature in the near future or go for the hack approach? Please advise. Thanks!
Just spent a few hours failing with this before finding out I was attempting the impossible (with current API). +1 from me please.
Please, add this feature.
On Thu, Jun 9, 2016 at 6:41 AM, Matt Beckett [email protected] wrote:
Just spent a few hours failing with this before finding out I was attempting the impossible (with current API). +1 from me please.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/driftyco/ionic/issues/5724#issuecomment-224847336, or mute the thread https://github.com/notifications/unsubscribe/AFrI2as2QYcLNfcuJ2GxtWruestEA19lks5qJ9_agaJpZM4HpsWx .
One thing to consider is how the Nav Stack will be re-created in different situations (these features may not exist now, but I'm guessing this is something Ionic may do in the future):
- Ionic App destroyed by OS to recover resources while user was using other apps, user opens the App again and Ionic restores the Nav Stack, returning the user to the Page they were on.
- Ionic App that supports deep-linking
In these situations, Ionic would need to recreate the Nav Stack based on Pages and NavParams. So if developers want this functionality in the future, all Page state should be derived from NavParams. And the values need to be serializable, probably just strings if you want deep-linking via a URL.
So based on this, I'm no longer in favor of my Promise idea or giving child pages a reference to the parent Page or a data container object.
I think @BugsBunnyBR original idea of being able to modify or add NavParams to the parent as you pop may be the way to go.
One could argue that you just shouldn't do this because it over-complicates Page state and navigation; instead developers should just handle such cases with Services that marshal data between Pages.
@jgw96, @adamdbradley and all ionic team, what the hell? This is a basic need for Ionic2, we are already in the beta9 and this is not even in the milestones. It`s nice to have buttons and menus that look like the native ones, but this issue is far more important! This is related to the basic architecture of a mobile application! I could wait for that bunch of nice things you all developed since this issue was raised, but missing this feature makes a app development far more complicated than it should.
@BugsBunnyBR Hey there! We understand your frustration. At the moment, while we want to do this, it would require heavy changes to the current navController API that could break many apps out there.
There are some alternative ways though that can help you.
-
Pass data to a DataService and having it be in charge of maintaining state.
-
Pass the original component along during the first navigation and modify the data needed then.
This is something we know we need to have, but we want to make sure we have a solid base first Sorry for being so quiet on this one!
@mhartington Don't worry about it :-)
I'm also in the camp that considers this feature a necessity, but I'd vastly prefer to wait and have it well made than to have it breaking things right now :-)
Thanks for all of your hard work Ionic Team :-)
And thanks for adding it to the milestones #^-^#
- Use a modal
- Implement your own service, as a singleton shared state.