[英]On Navigation Drawer back button is not working correctly?
我有5個片段,無論打開多少片段,按下Back鍵我都想移到主頁片段。
如果我從
[1]-> [2],然后按回[1]顯示的主頁片段
[1]-> [3],然后按回[1]顯示的主頁片段
[1]-> [4],然后按回[1]顯示的主頁片段
[1]-> [5],然后按回[1]顯示的主頁片段
如我所願。
但是問題是
[1]-> [2]-> [3],然后按[3]上的主片段重疊
[1]-> [2]-> [4],然后在[4]上按回原片段重疊
[1]-> [2]-> [5],然后按[5]上的主片段重疊
我僅將家庭片段添加到backStack並使用了replace片段方法。
為什么會這樣呢???
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.all_activity_layout); setToolbar(); addFragment(AttractionsFragment.newInstance()); setNavigationDrawer(); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.nav_attractions: if (mCurrentNavigationDrawerItem != 0) { mCurrentNavigationDrawerItem = 0; replaceFragment(AttractionsFragment.newInstance()); } break; case R.id.nav_packages: if (mCurrentNavigationDrawerItem != 1) { mCurrentNavigationDrawerItem = 1; replaceFragment(PackagesFragment.newInstance()); } break; case R.id.nav_passes: if (mCurrentNavigationDrawerItem != 2) { mCurrentNavigationDrawerItem = 2; replaceFragment(PassesFragment.newInstance()); } break; case R.id.nav_coupons: if (mCurrentNavigationDrawerItem != 3) { mCurrentNavigationDrawerItem = 3; replaceFragment(CouponsFragment.newInstance()); } break; case R.id.nav_more: if (mCurrentNavigationDrawerItem != 4) { mCurrentNavigationDrawerItem = 4; replaceFragment(MoreFragment.newInstance()); } break; } mNavigationView.getMenu().getItem(mCurrentNavigationDrawerItem).setChecked(true); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } public void replaceFragment(Fragment fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Get Current Visible fragment Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container); // Add to back stack only if it is AttractionsFragment if (f instanceof AttractionsFragment) { transaction.addToBackStack(fragment.getClass().getName()); } transaction.replace(R.id.fragment_container, fragment); transaction.commit(); Log.d("Navigation", "BackStack Count:" + getSupportFragmentManager().getBackStackEntryCount()); } public void addFragment(Fragment fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.fragment_container, fragment, "AttractionsFragment"); transaction.commit(); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } }
您需要先彈出第二個片段,然后才能導航到第三個片段。
Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
// Add to back stack only if it is AttractionsFragment
if (f instanceof AttractionsFragment) {
transaction.addToBackStack(fragment.getClass().getName());
} else {
activity.getSupportFragmentManager().popBackStackImmediate();
}
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
刪除transaction.addToBackStack(fragment.getClass().getName());
行,它將正常工作。 請檢查下面的代碼
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Get Current Visible fragment
Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
// Add to back stack only if it is AttractionsFragment
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
Log.d("Navigation", "BackStack Count:" + getSupportFragmentManager().getBackStackEntryCount());
}
將容器視圖替換為第一個片段:
@Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.fragment_container, new **your_first_fragment**()).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
如果要在同一活動中添加/啟動所有三個片段,請使用FragmentTransaction的replace()方法(用Fragment3替換Fragment2)來代替FragmentTransaction的add()方法來顯示Fragment3。 replace方法在添加新片段之前從后堆棧中刪除當前片段。 如果要從其他活動中啟動Fragment3,因此不想/不希望使用replace(),請在開始新活動之前從Backstack中刪除Fragment2(這會添加fragment3):
// in Fragment2, before adding Fragment3:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.remove(this) // "this" refers to current instance of Fragment2
.commit();
fragmentManager.popBackStack();
// now go ahead and launch (add) fragment3
// if fragment3 is launched from a different activity,
// start that activity instead
fragmentManager.beginTransaction()
.add(R.id.a_container_view_in_activity, new Fragment3(),
Fargment3.FRAGMENT3_ID)
.commit();
這樣可以解決您的問題。 嘗試這個..
嘗試這個:
在您的onBackPressed
:
@Override
public void onBackPressed(){
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
Log.i("MainActivity", "popping backstack");
fm.popBackStack();
} else {
Log.i("MainActivity", "nothing on backstack, calling super");
super.onBackPressed();
}
}
也可以在您的替換片段中解決重疊的問題
transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment);
要么
getSupportFragmentManager().beginTransaction().replace(R.id.container,new FirstFragment()).commit();
嘗試在替換片段中更改代碼:
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Get Current Visible fragment
//Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
AttractionsFragment myFragment = (AttractionsFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
// Add to back stack only if it is AttractionsFragment
transaction.addToBackStack(null);
}
transaction.replace(R.id.fragment_container, fragment, "MY_FRAGMENT");
transaction.commit();
Log.d("Navigation", "BackStack Count:" + getSupportFragmentManager().getBackStackEntryCount());
}
在OnBackpress
添加以下代碼:
@Override
public void onBackPressed() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (getFragmentManager().findFragmentById(R.id.activity_container) instanceof HomeFragment) {
super.onBackPressed();
} else {
replaceFragment(this, new HomeFragment());
}
}
並使用下面給出的代碼在所有情況下更改您的replaceFragment
和AddFragment
方法:
public void addFragment(final Activity mActivity, final Fragment newFragment, final Fragment hideFragment) {
final FragmentManager fragmentManager = mActivity.getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(hideFragment);
fragmentTransaction.add(R.id.activity_container, newFragment, newFragment.getClass().getSimpleName());
fragmentTransaction.addToBackStack(hideFragment.getClass().getSimpleName());
fragmentTransaction.commitAllowingStateLoss();
}
public void replaceFragment(final Activity mActivity, final Fragment newFragment) {
final FragmentManager fragmentManager = mActivity.getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.activity_container, newFragment, newFragment.getClass().getSimpleName());
fragmentTransaction.commit();
}
其中activity_container
是activity_container
的xml文件中的FrameLayout
ID。
在活動的onCreate
,調用replaceFragment
方法而不是addFragment
。 並在所有導航項目的click事件中,調用replaceFragment
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.