How to exit the app by pressing back button

Dear, Fuse team.

I want to use back press button to exit the app. So I used tag and called a function like this.

[code in MainView.ux]

[function]
function backButtonClicked(obj, KeyEventArgs){
debug_log("***************************** menu button clicked");
currentPage.value = mainPage;
}

However, when I press the back button, some error occur like below…

LOG: ***************************** menu button clicked
ERROR: Object reference was null
Uno.NullReferenceException occured.
at Fuse.Navigation.Router.GoUp()
at Fuse.Navigation.Router.GoBack()
at Fuse.Navigation.Router.OnKeyPressed(object,Fuse.Input.KeyEventArgs)
at Fuse.Input.KeyPressed.Invoke(Fuse.Input.KeyPressedHandler,object,Fuse.Input.KeyPressedArgs)
at Fuse.VisualEvent2.InvokeInternal(THandler,object,TArgs) at Fuse.VisualEvent2.InvokeGlobalHandlers(Fuse.Visual,TArgs)
at Fuse.VisualEvent2.Raise(TArgs,Fuse.VisualEventMode,bool,[Uno.Action<TArgs, Uno.Collections.IList<Fuse.Visual>>]) at Fuse.VisualEvent2.RaiseWithBubble(TArgs,[Fuse.VisualEventMode])
at Fuse.Input.Keyboard.RaiseKeyPressed(Uno.Platform.Key,bool,bool,bool,bool)
at Fuse.KeyboardBootstrapper.OnKeyPressed(object,Uno.Platform.KeyEventArgs)
at Uno.Platform2.Application.OnKeyDown(Uno.Platform.Key,Uno.Platform.EventModifiers)

How can I use the tag to exit the app?
Moreover, Is there any back button press example?

Thanks,
Judy.

Hi!

Could you share some more of your code please? Its impossible to say what when wrong based on the tiny snippet you’ve provided.

There is unfortunately currently no built in component that lets you quit the app using the back button, but it should be possible to create using foreign code (https://www.fusetools.com/docs/native-interop/foreign-code). This is a bit advanced however, so depending on how comfortable you are with programming it might be difficult. The community is however always quite helpful if you decide to give it a try :slight_smile:

Hi team!
I uploaded my code in Git Hub.

Could you please confirm it?

Thanks & Regards,
LEE.

There’s actually an older thread about this here and it resulted in Bolav publishing this :slight_smile:

Hi LEE

I’ve looked at your code, and it seems the crash is cause by a bug on our end. Thanks for reporting :slight_smile: You should however consider not using DirectNavigation as it represents an older way of doing navigation in Fuse. I would suggest you take a look at our tutorial series: https://www.fusetools.com/docs/tutorial/tutorial

You can also get inspiration from this example: https://github.com/fusetools/fuse-samples/tree/master/Samples/cattr

The actual problem that causes the crash is that you are not actually using the Router that you have defined. When you try to goBack it doesn’t have any navigation history to go back to. (this shouldn’t cause a crash though)