gpt4 book ai didi

java - 如何在Android上的fragment中使用onBackPress按钮

转载 作者:行者123 更新时间:2023-12-02 09:27:19 24 4
gpt4 key购买 nike

在我的应用程序中,我有NavigationView,并且在这个NavigationView中,我设置了菜单来打开一些 fragment !

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="320dip"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#000"
android:fitsSystemWindows="true"
app:headerLayout="@layout/tester_drawer_header"
app:itemIconTint="#fff"
app:itemTextColor="#fff"
app:menu="@menu/tester_drawer_main"/>

我希望在点击此菜单时打开一个 fragment ,并且我希望在进入嵌套 fragment 并按onBack按钮打开第一个fragment不打开backStackfragments!

我写了下面的代码,但是在点击后退按钮后,从backStack返回fragments,但我想先打开 fragment !

我的 Activity 代码:

public class TesterDashboardActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tester_dashboard);

loadFragment(new TesterTestListFragment(), userName);
}

@Override
public void onBackPressed() {
fragmentBackCount = getSupportFragmentManager().getBackStackEntryCount();

if (fragmentBackCount <= 1) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
//finishAffinity();
super.onBackPressed();
}
} else {
getSupportFragmentManager().popBackStack();
}
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_home:
loadFragment(new TesterTestListFragment(), userName);
break;
case R.id.nav_billing:
loadFragment(new TesterFinanceFragment(), context.getString(R.string.menu_billing));
break;
case R.id.nav_edit_profile:
loadFragment(new TesterProfileFilterEditFragment(), context.getString(R.string.menu_edit_profile));
break;
case R.id.nav_bank:
loadFragment(new TesterShebaFragment(), context.getString(R.string.menu_bank));
break;
case R.id.nav_report:
loadFragment(new TesterReportsFragment(), context.getString(R.string.menu_report));
break;
case R.id.nav_setting:
loadFragment(new TesterSettingFragment(), context.getString(R.string.menu_setting));
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}

public void loadFragment(Fragment fragment, String toolbarTitle) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.testerFragment, fragment,TAG_FRAGMENT);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

toolbar.setTitle(toolbarTitle);
}

如何修复它?

最佳答案

在您的代码中,我发现当 fragmentBackCount>1 时,您只需调用 getSupportFragmentManager().popBackStack(); 一次,因此您将在后台看到顶部 fragment 。 我认为你应该调用getSupportFragmentManager().popBackStack();,fragmentBackCount-1时间到达第一个fragmnet

 else {
for(int i=0;i<fragmentBackCount-1;i++)
getSupportFragmentManager().popBackStack();
}

关于java - 如何在Android上的fragment中使用onBackPress按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com