如何先关闭抽屉然后调用onBackPressed?
How to close drawer first then call onBackPressed?
我有一个带抽屉布局的activity。
我想检查一下抽屉是否关上了然后打电话给onBackPressed
。如果抽屉打开,它应该先关闭,然后 onBackPressed
应该被调用。
我尝试使用 addDrawerListener 这样做
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpUI();
listeners(
}
public void setUpUI() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
}
public void listeners()
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
onBackPressed();
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
@Override
public void onBackPressed()
drawerLayout.closeDrawer(Gravity.END);
if (!mBackPressCancelled) {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStackImmediate();
} else {
super.onBackPressed();
}
}
}
}
有了这个抽屉,关闭了一半的应用程序。我想关闭抽屉调用onBaackPressed。
我该怎么做?请帮忙。谢谢。
您可以使用
检查抽屉是否关闭
if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
//drawer is open
}else{
super.onBackPressed();
}
这将是您的代码:
@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END)
} else {
super.onBackPressed();
}
}
我有一个带抽屉布局的activity。
我想检查一下抽屉是否关上了然后打电话给onBackPressed
。如果抽屉打开,它应该先关闭,然后 onBackPressed
应该被调用。
我尝试使用 addDrawerListener 这样做
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpUI();
listeners(
}
public void setUpUI() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
}
public void listeners()
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
onBackPressed();
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
@Override
public void onBackPressed()
drawerLayout.closeDrawer(Gravity.END);
if (!mBackPressCancelled) {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStackImmediate();
} else {
super.onBackPressed();
}
}
}
}
有了这个抽屉,关闭了一半的应用程序。我想关闭抽屉调用onBaackPressed。
我该怎么做?请帮忙。谢谢。
您可以使用
检查抽屉是否关闭if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
//drawer is open
}else{
super.onBackPressed();
}
这将是您的代码:
@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END)
} else {
super.onBackPressed();
}
}