MonoDroid MVVMCross handle back button pressed on a TabHost-Collection of common programming errors
I am using the MvvmCross framework to build a cross platform (iOS, Android, WP7) application. I am currently trying to handle the back button pressed on Android from a TabHost Activity. The TabHost Activity will have been started from another Activity(that isn’t in the Tab selection) and I want the back button to close the application, however when the back button is pressed I am returning to the previous screen.
The ‘NoHistory = “true”‘ doesn’t seem to work within the TabHost.
I am handling my back button press in the View as below but aren’t sure what can be called in the ViewModel to close the application.
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back)
{
return this.ViewModel.CloseThisApplication();
}
return base.OnKeyDown(keyCode, e);
}
Are there any methods that can be called to close the application? Or have a better idea on how this may be handled? Ideally the method could be replicated across the three platforms specified earlier.
Thanks in advance
-
I don’t think we can replicate an exitNowPlease call across all 3 platforms – as most of them discourage you from supplying an Exit feature – e.g. I believe the only way to exit in WindowsPhone7 is to throw an unhandled exception.
However, I think you should be able to achieve what you describe by:
- add the NoHistory=true flag to the start activity – the one before the tabhost. This
NoHistory=true
flag means don’t include this activity type in the back stack. - remove your back button handler.
The platform should then behave as you’ve described.
Also for cross platform clearing the back stack, see the ClearTop explanation in How to add an invisible ViewModel to the Navigation Stack in MvvmCross
- add the NoHistory=true flag to the start activity – the one before the tabhost. This