{"id":408,"date":"2020-05-12T11:29:45","date_gmt":"2020-05-12T03:29:45","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=408"},"modified":"2021-01-08T16:46:59","modified_gmt":"2021-01-08T08:46:59","slug":"android-%e7%9b%a3%e8%81%bd%e5%ba%95%e4%b8%8b%e7%9a%84home%e9%8d%b5%e5%92%8cmenu%e9%8d%b5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e7%9b%a3%e8%81%bd%e5%ba%95%e4%b8%8b%e7%9a%84home%e9%8d%b5%e5%92%8cmenu%e9%8d%b5\/","title":{"rendered":"Android \u76e3\u807d Back\u9375\u3001Home\u9375\u3001Menu\u9375"},"content":{"rendered":"<h1>Android \u76e3\u807d Back\u9375\u3001Home\u9375\u3001Menu\u9375<\/h1>\n<ul>\n<li>\u6587\u7ae0\u76ee\u9304<br \/>\n<\/p>\n<ul>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u76e3\u807d\u5e95\u4e0b\u7684home\u9375\u548cmenu\u9375\/fly\/android\/#a\">Back\u9375<\/a><br \/>\n<\/p>\n<ul>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u76e3\u807d\u5e95\u4e0b\u7684home\u9375\u548cmenu\u9375\/fly\/android\/#b\">Activity\u7528\u6cd5<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u76e3\u807d\u5e95\u4e0b\u7684home\u9375\u548cmenu\u9375\/fly\/android\/#c\">Fragment\u7528\u6cd5<\/a><br \/>\n<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u76e3\u807d\u5e95\u4e0b\u7684home\u9375\u548cmenu\u9375\/fly\/android\/#d\">Home\u9375&amp;Menu\u9375<\/a><br \/>\n<\/p>\n<ul>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u76e3\u807d\u5e95\u4e0b\u7684home\u9375\u548cmenu\u9375\/fly\/android\/#e\">\u5275\u5efa\u5ee3\u64ad\u63a5\u6536\u8005<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-\u76e3\u807d\u5e95\u4e0b\u7684home\u9375\u548cmenu\u9375\/fly\/android\/#f\">\u8a3b\u518a\u63a5\u6536\u8005<\/a><br \/>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h3>1.Back\u9375<\/h3>\n<p><a id=\"b\"><\/a><\/p>\n<h4>Activity\u7528\u6cd5<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">override fun onBackPressed() {\n    ToastUtil.toast(\"\u6514\u622a\u59b3\")\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>Fragment\u7528\u6cd5<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">override fun onAttach(context: Context) {\n    super.onAttach(context)\n\n    val callback = object : OnBackPressedCallback(true) {\n        override fun handleOnBackPressed() {\n            Toast.makeText(context, \"\u6514\u622a\u59b3\", Toast.LENGTH_SHORT).show()\n        }\n    }\n    requireActivity().onBackPressedDispatcher.addCallback(this, callback)\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h3>2.Home\u9375&amp;Menu\u9375<\/h3>\n<p><a id=\"e\"><\/a><\/p>\n<h4>\u5275\u5efa\u5ee3\u64ad\u63a5\u6536\u8005<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class KeyReceiver : BroadcastReceiver() {\n    val REASON = \"reason\"\n    val HOMEKEY = \"homekey\"\n    val RECENTAPPS = \"recentapps\"\n\n    override fun onReceive(context: Context?, intent: Intent?) {\n        intent?.apply {\n            val action = intent.action\n            if (action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {\n                val reason = intent.getStringExtra(REASON)\n\n                if (TextUtils.equals(reason, HOMEKEY)) {\n                    Toast.makeText(context, \"HOME\", Toast.LENGTH_SHORT).show()\n                } else if (TextUtils.equals(reason, RECENTAPPS)) {\n                    Toast.makeText(context, \"MENU\", Toast.LENGTH_SHORT).show()\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"f\"><\/a><\/p>\n<h4>\u8a3b\u518a\u63a5\u6536\u8005<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">override fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n    setContentView(R.layout.activity_main)\n\n    registerReceiver(KeyReceiver(), IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>override fun onCreate(savedInstanceState: Bundle?) {<br \/>\n    super.onCreate(savedInstanceState)<br \/>\n    setContentView(R.layout.activity_main)<\/p>\n<p>    registerReceiver(KeyReceiver(), IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))<br \/>\n}<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5,165],"tags":[13,70,15],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-android","category-165","tag-android","tag-keyguard","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=408"}],"version-history":[{"count":24,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/408\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}