[英]onbackpress go to home screen instead of previous activity
我有一個應該在后按時關閉的導航抽屜,這很正常,但是當導航抽屜關閉並且有第二次后退按鈕按下時,應用會關閉而不是返回主屏幕
這是我正在嘗試的代碼
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
drawerLayout.closeDrawer(Gravity.LEFT);
}else{
//super.onBackPressed();
Intent mainActivity = new Intent(Intent.ACTION_MAIN);
mainActivity.addCategory(Intent.CATEGORY_HOME);
mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainActivity);
}
我們如何關閉導航抽屜然后直接轉到主屏幕/主要活動
我不想用
Intent i = new Intent(AccountActivity.this, HomeActivity.class);
startActivity(i);
從家里開始其他活動時,請不要使用finish()方法。
//從主屏幕刪除finish方法。
首先,請閱讀並理解我的回答,以清除MainActivity
的后備堆棧。 我的方法只有在您了解這一點后才能起作用。
因此,除非您先返回主屏幕並關閉主屏幕的“ Activity
,否則您將無法直接關閉應用程序(請參閱此問題 )。
CurrentActivity
:
if(drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.closeDrawer(Gravity.LEFT);
}else{
// After you understood my answer above (I link it with "here"),
// you just need this call:
startActivity(new Intent(CurrentActivity.this, HomeScreenActivity.class));
}
注意:在onResume()
方法中調用finish()
可能會導致問題。
在HomeScreenActivity
內部的onResume()
方法中調用finish()
:
@Override
protected void onResume() {
super.onResume();
HomeScreenActivity.this.finish();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.