{"id":1890,"date":"2022-08-30T15:20:18","date_gmt":"2022-08-30T15:20:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/monodroid-mvvmcross-handle-back-button-pressed-on-a-tabhost-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:18","modified_gmt":"2022-08-30T15:20:18","slug":"monodroid-mvvmcross-handle-back-button-pressed-on-a-tabhost-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/monodroid-mvvmcross-handle-back-button-pressed-on-a-tabhost-collection-of-common-programming-errors\/","title":{"rendered":"MonoDroid MVVMCross handle back button pressed on a TabHost-Collection of common programming errors"},"content":{"rendered":"<p>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&#8217;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.<\/p>\n<p>The &#8216;NoHistory = &#8220;true&#8221;&#8216; doesn&#8217;t seem to work within the TabHost.<\/p>\n<p>I am handling my back button press in the View as below but aren&#8217;t sure what can be called in the ViewModel to close the application.<\/p>\n<pre><code>public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)\n    {\n        if (keyCode == Keycode.Back)\n        {\n\n            return this.ViewModel.CloseThisApplication();\n\n        }\n        return base.OnKeyDown(keyCode, e);\n    }\n<\/code><\/pre>\n<p>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.<\/p>\n<p>Thanks in advance<\/p>\n<ol>\n<li>\n<p>I don&#8217;t think we can replicate an exitNowPlease call across all 3 platforms &#8211; as most of them discourage you from supplying an Exit feature &#8211; e.g. I believe the only way to exit in WindowsPhone7 is to throw an unhandled exception.<\/p>\n<p>However, I think you should be able to achieve what you describe by:<\/p>\n<ol>\n<li>add the NoHistory=true flag to the start activity &#8211; the one before the tabhost. This <code>NoHistory=true<\/code> flag means don&#8217;t include this activity type in the back stack.<\/li>\n<li>remove your back button handler.<\/li>\n<\/ol>\n<p>The platform should then behave as you&#8217;ve described.<\/p>\n<p>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<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:11:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;t in the Tab selection) and I want the back button to close the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1890","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1890"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1890\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}