android-fragments 如何在使用底部导航按钮切换时不删除打开的片段

1cklez4t  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(135)

在下面的代码中,我们有一个TrendsFragment和一个ProfileFragment作为基础片段,它们将通过底部导航视图打开。我可以打开一个空白片段,并从这两个片段导航到它。当我执行TrendsFragment-profileFragment-BlankFragment-TrendsFragment时,BlankFragment被破坏了。我尝试了很多方法来改变它,但是我无法管理它。我怎样才能使BlankFragment活起来呢?我开始使用导航组件库,因为文档中说:
“NavigationUI类包括当用户在菜单项之间移动时自动保存和还原菜单项状态的API。在下列情况下,这些API默认实现多个返回堆栈支持:
当您使用setupWithNavController()的适当重载将NavigationView或BottomNavigationView的示例与NavController示例关联时,如添加导航抽屉或底部导航中所述。当您使用onNavDestinationSelected()创建绑定到NavController示例承载的目标的自定义导航菜单UI时。”
我做错了什么?请解释清楚,提前感谢...

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ActivityMainBinding binding;
NavController navController;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    NavHostFragment navHostFragment=(NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_container);
    navController = navHostFragment.getNavController();
    NavigationUI.setupWithNavController(binding.forumBottomNav, navController);
}

}

这是我的导航图

<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/trendsFragment">

<fragment
    android:id="@+id/profileFragment"
    android:name="com.yusufemirbektas.bottomnavfrags.ProfileFragment"
    android:label="fragment_profile"
    tools:layout="@layout/fragment_profile" >
    <action
        app:restoreState="true"
        android:id="@+id/action_profileFragment_to_blankFragment"
        app:destination="@id/blankFragment" />
</fragment>
<fragment
    android:id="@+id/trendsFragment"
    android:name="com.yusufemirbektas.bottomnavfrags.TrendsFragment"
    android:label="fragment_trends"
    tools:layout="@layout/fragment_trends" >
    <action
        app:restoreState="true"
        android:id="@+id/action_trendsFragment_to_blankFragment"
        app:destination="@id/blankFragment" />
</fragment>
<fragment
    android:id="@+id/blankFragment"
    android:name="com.yusufemirbektas.bottomnavfrags.BlankFragment"
    android:label="fragment_blank"
    tools:layout="@layout/fragment_blank" >
    <action
        app:restoreState="true"
        android:id="@+id/action_blankFragment_self"
        app:destination="@id/blankFragment" />
</fragment>

趋势片段

public class TrendsFragment extends Fragment {
FragmentsListener listener;
FragmentTrendsBinding binding;
FragmentManager fm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fm = getChildFragmentManager();
}

public void setListener(FragmentsListener listener) {
    this.listener = listener;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    binding = FragmentTrendsBinding.inflate(inflater, container, false);
    return binding.getRoot();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    NavController navController= Navigation.findNavController(view);
    binding.openFragButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            navController.navigate(R.id.action_trendsFragment_to_blankFragment);
        }
    });
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    fm = null;
    listener = null;
}
}

轮廓片段

public class ProfileFragment extends Fragment {

private static final String TAG = "ProfileFragment";
FragmentProfileBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    binding=FragmentProfileBinding.inflate(inflater,container,false);

    return binding.getRoot();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    NavController navController= Navigation.findNavController(view);
    binding.openFragButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            navController.navigate(R.id.action_profileFragment_to_blankFragment);
        }
    });
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding=null;
}

@Override
public void onDestroy() {
    super.onDestroy();

}
}

空白片段

public class BlankFragment extends Fragment {

FragmentBlankBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    binding = FragmentBlankBinding.inflate(inflater, container, false);
    return binding.getRoot();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    NavController navController= Navigation.findNavController(view);
    binding.openFragButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            navController.navigate(R.id.action_blankFragment_self);
        }
    });
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
}

@Override
public void onDestroy() {
    super.onDestroy();
}
}
btxsgosb

btxsgosb1#

您需要将每个导航图目标设置为顶级目标。

BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.fragment_profile, R.id.fragment_blank, R.id.fragment_trends)
        .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.forumBottomNav, navController);

相关问题