[英]Navigation drawer with fragment and on back pressed
I have a navigation drawer with only one activity and several fragments. 我有一个只有一个活动和几个片段的导航抽屉。 My main fragment is really heavy to load so I try to keep it in memory because if I don't do it the drawer will be laggy when I use it.
我的主要片段确实很重,因此我尝试将其保留在内存中,因为如果不这样做,则在使用时,抽屉会变得笨拙。
To do so I have a layout like this : 为此,我有一个这样的布局:
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<fragment
android:id="@+id/main_fragment"
android:name=".MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
They they the same place but in the code I replace the fragment layout with lighter fragment and show/hide the main fragment. 它们在同一位置,但是在代码中,我用较浅的片段替换了片段布局,并显示/隐藏了主要片段。 This is a showFragment method to is called when I click on the item in the drawer.
当我单击抽屉中的项目时,这是一个showFragment方法。
case 0: //Light Fragment
fragment = getSupportFragmentManager().findFragmentByTag("light");
if (fragment == null) {
fragment = ProfileFragment.newInstance();
}
getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager().findFragmentById(R.id.main_fragment)).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment, "light").addToBackStack(null).commit();
break;
case 1: //Heavy Frag
if (getActiveFragment() != null) {
getSupportFragmentManager().beginTransaction().remove(getActiveFragment()).commit();
}
getSupportFragmentManager().beginTransaction().show(getSupportFragmentManager().findFragmentById(R.id.main_fragment)).addToBackStack(null).commit();
break;
Which works fine except when I push the back button. 除按下后退按钮外,其他方法均正常。 I try many implementation of the onBackPressed but I always end up with either the main fragment showing when it should be hidden (So 2 fragment in top of each other) and the main fragment simply doesn't show.
我尝试了onBackPressed的许多实现,但是我总是以显示何时应隐藏主片段的方式结束(所以2个片段彼此重叠),而主要片段根本不显示。
Bonus question: Is that the correct way of doing thing ? 额外的问题:这是正确的做事方式吗? I mean to avoid the drawer to lag or should i completely change my implementation?
我的意思是避免抽屉滞后,还是应该完全改变我的实现方式?
Thanks. 谢谢。
Well, this should be more of a comment but since I cannot comment, I have to answer. 好吧,这应该更多是评论,但由于我无法发表评论,所以我必须回答。
The reason could be that the fragments are not going into the backstack properly or in the order as they should. 原因可能是碎片未正确或按应有的顺序进入后堆栈。
I once created a project wherein I used Navigation Drawer with fragments. 我曾经创建一个项目,在其中使用带有片段的导航抽屉。 If I understand you correctly, what you want is to go to the last open fragment.
如果我对您的理解正确,那么您要转到最后一个打开的片段。
Try using the following code: Put it in onBackPressed() 尝试使用以下代码:将其放在onBackPressed()中
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
this.finish();
} else {
getFragmentManager().popBackStack();
}
}
Essentially, you are popping the last fragment that was added to the backstack. 本质上,您是在弹出添加到后堆栈的最后一个片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.