2016-11-22 220 views
0

我的onBackPressed()將我從我的應用程序中取出。我怎樣才能避免這種情況?無論是它應該送我回到最後顯示的片段或只是送我到一個特定的片段。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(); 
    } 
} 
+0

添加片段以通過重新加載片段來手動堆疊或管理它 –

回答

1

試試這個,如果你使用的是碎片android.support.v4.app.Fragment。

private boolean allowExit = false; 

然後

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } 
    else if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
     // handling fragment backbutton navigation 
     getSupportFragmentManager().popBackStack(); 
    } else { 
     if (allowExit) { 

      super.onBackPressed(); 
     } else { 
      Toast.makeText(this, getString(R.string.app_exit_message), Toast.LENGTH_SHORT).show(); 
      allowExit = true; 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        allowExit = false; 
       } 
      }, 3000); 
     } 
    } 
} 

如果使用不支持分片必須更換

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } 
    else if (getFragmentManager().getBackStackEntryCount() > 0) { 
     // handling fragment backbutton navigation 
     getFragmentManager().popBackStack(); 
    } else { 
     if (allowExit) { 

      super.onBackPressed(); 
     } else { 
      Toast.makeText(this, getString(R.string.app_exit_message), Toast.LENGTH_SHORT).show(); 
      allowExit = true; 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        allowExit = false; 
       } 
      }, 3000); 
     } 
    } 
} 

並添加消息到string.xml

<string name="app_exit_message">Press again to exit</string> 
0

請嘗試下面的代碼。
1.如果抽屜打開,它將關閉抽屜。
2.如果任何片段在backstack中,它將導航到前一個片段。
3.如果No Fragment在backstack中,它將關閉應用程序。

 @Override 
     public void onBackPressed() { 
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      if (drawer.isDrawerOpen(GravityCompat.START)) { 
       drawer.closeDrawer(GravityCompat.START); 
      } else { 
       oneStepBack(); 
      } 
     } 

     /** 
     * function to go back to previous fragment 
     */ 
     private void oneStepBack() { 
      FragmentTransaction fts = getSupportFragmentManager().beginTransaction(); 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      showOutput("Back Stack Count>>>>>>>>>>" + fragmentManager.getBackStackEntryCount()); 
      if (fragmentManager.getBackStackEntryCount() >= 2) { 
       fragmentManager.popBackStackImmediate(); 
       fts.commit(); 
      } else { 
       finish(); 
      } 
     } 
0
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } 
    else{ 

     write your code here what activity you want to open on back press and remove below line 
     //super.onBackPressed(); 
    } 
}