Prevent view from closing by code

I try to find a method to prevent the view from closing (like the VB.Form_QueryUnload Event) . It could be done in the view’s Deactivate Event.
The reason is to ask for saving dirty data before the view is closed.
Any suggestions how to solve this?

Hi Hans,

That is more a “desktop experience” than a mobile one. Said that… I think that you can implement such behavior on the Deactivate Event Handler. The view is going to close, but you have a chance to save the changes in the data of the view.

Javier Rodríguez
Evangelista Xojo en Español
Autor del Libro “Programación Multiplataforma Xojo

Use the CancelClose event. It fires before the Close event and if you return true from CancelClose, the window stays open.

True, but that is for desktop apps. This is related to iOS, at least according to the channel.

iOS has no “CancelClose” and not even sure why it has “Close”?
When would IT fire in relation to DEACTIVATE which to me is the only one that would make sense?

Preventing a view from closing would not prevent the system to hide it when the user presses the home button.

What exactly are you trying to achieve ?

I want the user give the choice to save the entered data before leaving a view because it is lost by accidently tapping on the Backbutton.
I can not save it mandatory - the saving hast to be the users choice.

Xojo fires the deactivated event before the close event. My idea was to prevent from closing in the deactivated event.
Seems that this is not intended in the iOS Philosophy so i have to solve this by design.
I think i will set a flag for the parentview that it push the subview again with the preserved data after asking for It.

In my app I don’t prevent the user from tapping the back button, but I do save the data when the user does this. To do that I add an Event Definition to my view and then use AddHandler to provide a method to save the child view’s data within the parent (calling) view. Perhaps you could use a similar method but instead of just saving the data you could prompt the user to save? Regardless this will occur after the child view has closed so you would still need to deal with this perhaps by pushing the child view again if you wanted the user to go on editing. It sounds like you’re on the right track anyway but I just thought this might be helpful. :slight_smile:

Thanks Jason, i’ll figure out whats the best solution for this issue - i think i’m still thinking too much like a desktop developer ;-))

I’ve solved this issue by creating my own NavigationBar. Now i have control over the behavior ;-))

In addition, you should save in deactivate, because the user can always press the home button and it effectively freezes the app instead of quitting.

Michel, thank you for this advice, but:
Pressing the home button will leave the app in its current state. I only want to take an action if the user leaves a subview by pressing the Backbutton in the Navigation Bar. I removed the Navigation Bar by design and then added a custom Navigation Bar with a Custom Backbutton, Title etc. so i can do whatever i want if the Backbutton is pressed.

Did i oversee something?

[quote=216068:@Hans-Jürgen Müller]Michel, thank you for this advice, but:
Pressing the home button will leave the app in its current state. I only want to take an action if the user leaves a subview by pressing the Backbutton in the Navigation Bar. I removed the Navigation Bar by design and then added a custom Navigation Bar with a Custom Backbutton, Title etc. so i can do whatever i want if the Backbutton is pressed.

Did i oversee something?[/quote]

If it is important to save the data, let us imagine instead of moving away from the view with your navigation bar, the user simply presses the home button. Then your app will not have saved the data, and if the user then restarts the phone or double press the home button and slides up the app (quit it), you will have never saved your precious data. Hence my advice.

Never trust the user will use your app as you plan.

If the user not only press the Home button but also “kills” your app (double tap on the home button and sliding your app preview)… then the state is lost what includes the unsaved data.

The way apps usually behave under iOS is saving the state every time the user changes data. If you really need to offer the option of saving or discarding the changes to the user, then maybe would like consider the inclusion of a “Save” button inside the view or something like that.

Javier Rodríguez
Evangelista Xojo en Español
Autor del Libro “Programación Multiplataforma Xojo

Oh yes, you’re right - there is already a Save Button, but my testers reported that they sometimes tap the Backbutton accidentally and the entered data was lost. I have to say that this is an App for Golfers and they wearing gloves, so mishitting the screen is not very rare.

Quitting the app is not done accidentally (in most cases) so i don’t care about this.

Thank you all for your advices - this conversation let me find the right solution for my problem.
Sometimes you have to talk about a problem to see an alternative way to solve it ;-))

Why not have it the way most iOS apps have “record entry form” dialogs?

[code][Cancel] MyGolfApp [Done]

Label: [ TextField … ]
Label: [ TextField … ]
Label: [ TextField … ]
…[/code]
Then it is clear for the user that he does not only go back, he also cancels the entry.

Sorry Eli, i talked about * accidentally * mishitting (tapping) the screen - not about an UI that can be misunderstood ;-))
The players are wearing their iPhones sometimes in the Jacket or Bag or somewhere else and if they take it out they sometimes tap the screen accidentally. During playing the App is running and if they forgot to switch off the screen then they tapped sometimes the Back(cancel) Button.