我有一個抽屜佈局的活動。如何先關閉抽屜,然後調用onBackPressed?
我想檢查抽屜是否關閉,然後致電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();
}
}
}
}
有了這個抽屜靠攏半應用進行了封閉。我想關閉抽屜,打電話給BaackPressed。
我該怎麼做?請幫忙。謝謝。
發表您的錯誤的logcat –
我沒有得到任何錯誤@ RahulKhurana – Sid