当按下后退按钮并打开导航抽屉时,我的应用程序关闭而不是关闭抽屉

Posted

技术标签:

【中文标题】当按下后退按钮并打开导航抽屉时,我的应用程序关闭而不是关闭抽屉【英文标题】:When back button is pressed and navigation drawer is open, my app closes instead of closing drawer 【发布时间】:2016-12-05 16:34:44 【问题描述】:

我正在尝试在我的应用中实现材料设计,并且正在学习教程。我遇到的问题是当导航抽屉打开时,我点击手机上的“后退”按钮,它会关闭我的应用程序而不是关闭抽屉。如何关闭抽屉而不是退出应用程序?谢谢。

这是代码

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener 

private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch
    displayView(0);



@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in androidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) 
        return true;
    

    if(id == R.id.action_search)
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    

    return super.onOptionsItemSelected(item);


@Override
public void onDrawerItemSelected(View view, int position) 
    displayView(position);


private void displayView(int position) 
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) 
        case 0:
            fragment = new HomeFragment();
            title = getString(R.string.title_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.title_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.title_messages);
            break;
        default:
            break;
    

    if (fragment != null) 
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
       // getSupportActionBar().setTitle(title);
    


这里是布局xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>


<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_
        android:layout_
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_
        android:layout_
        android:layout_weight="1" />


</LinearLayout>


<fragment
    android:id="@+id/fragment_navigation_drawer"
     android:name="test.materialdesign.com.materialdesign.activity.FragmentDrawer"
    android:layout_
    android:layout_
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

你的意思是你想在后按时关闭导航抽屉? 是的,我就是这个意思。目前,它只是退出应用程序。 【参考方案1】:

要关闭抽屉,当按下返回按钮时,你必须覆盖onBackPressed(),这样:

@Override
public void onBackPressed() 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) 
        drawer.closeDrawer(GravityCompat.START);
     else 
        super.onBackPressed();
    

【讨论】:

【参考方案2】:

实现 onBackpress() 并试试这个例子

@Override
public void onBackPressed() 

    int count = fragmentManager.getBackStackEntryCount();

    if (count == 1) 
        fragmentManager.popBackStackImmediate();
                          
       

【讨论】:

【参考方案3】:

你必须重写成员函数onBackPressed

@Override
public void onBackPressed() 
  if(drawerLayout.isDrawerOpen(Gravity.LEFT)) 
    drawerLayout.closeDrawer(Gravity.LEFT);
   else 
    super.onBackPressed();
  

【讨论】:

以上是关于当按下后退按钮并打开导航抽屉时,我的应用程序关闭而不是关闭抽屉的主要内容,如果未能解决你的问题,请参考以下文章

如何使向上按钮返回而不是打开导航抽屉

导航抽屉,处理后退按钮以转到以前的片段?

按下后退按钮时,自动折叠移动设备上的下拉导航栏菜单

如何使用导航抽屉打开和关闭更改按钮图像

后退按钮关闭应用程序而不是转到上一个片段 android 导航组件

如何处理后退按钮android以退出应用程序并在首页中打开时关闭本机导航侧菜单