Drawerlayout + Toolbar Press the return key to close the application instead of the menu solution.

Source: Internet
Author: User

First of all, about Drawerlayout, he is a view that can be used to make a menu, where his first view is the content area, and then he can have two view, depending on whether their Layout_gravity property is right or left menu, The left side is equal to right, which is the side menu. The General menu is a ListView.

We used toolbar instead of the original actionbar to let him use it with drawerlayout. Click on the toolbar of the navigation icon can show or hide the menu, as well as the effect, similar to the app, then there is a problem, when we click on the Display menu, if you click the Return button, then he will launch the application instead of closing the menu, this is not the result we want, There are two ways to solve it.

One, we are through actionbardrawertoggle to listen to the open shutdown of drawerlayout, so we can rewrite actionbardrawertoggleondrawerclosed and Ondraweropend methods , and then override the OnKeyDown method to set a flag. The specific code is as follows:

Mactionbardrawertoggle = new Actionbardrawertoggle (this, mdrawerlayout, Mtoolbar,    R.string.open, R.string.close) {@Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (drawerview); flag = false;} @Overridepublic void ondraweropened (View drawerview) {super.ondraweropened (drawerview); flag = True;}}; Mactionbardrawertoggle.syncstate (); Mdrawerlayout.setdrawerlistener (Mactionbardrawertoggle);

public boolean onKeyDown (int keycode, keyevent event) {  if (flag)  {  mdrawerlayout.closedrawers ();  return true;  }  Return Super.onkeydown (KeyCode, event);}

This will solve the problem.

The second solution, because our left menu (mine) is made using fragment, can be achieved through his hid and show methods.

The specific code is as follows:

Mactionbardrawertoggle = new Actionbardrawertoggle (this, mdrawerlayout, Mtoolbar,    R.string.open, R.string.close) {@Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (drawerview); flag = false;} @Overridepublic void ondraweropened (View drawerview) {super.ondraweropened (Drawerview); Fragmentmanager fm = Getsupportfragmentmanager ();  Fm.begintransaction (). Show (Mleftmenufragment). commit (); flag = True;}}; Mactionbardrawertoggle.syncstate (); Mdrawerlayout.setdrawerlistener (Mactionbardrawertoggle);

public boolean onKeyDown (int keycode, keyevent event) {  if (flag)  {  Fragmentmanager fm = Getsupportfragmentmanager ();  Fm.begintransaction (). Hide (Mleftmenufragment). commit ();  return true;  }  Return Super.onkeydown (KeyCode, event);}


The first one is recommended, the second kind of personal test will have a lag, click the Back button and then click the Menu button, the menu content will appear for a while, the first kind will not.


Drawerlayout + Toolbar The solution of closing the application instead of the menu by pressing the return key.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.