简体   繁体   English

如果该片段已打开,如何防止从导航抽屉中打开该片段

[英]How to prevent opening a fragment from navigation drawer if that fragment is already opened

How to prevent opening a fragment from navigation drawer if that fragment is already opened, when the fragment is opened and click in the NavigationDrawer item the fragment reCreates again, So how to check if the fragment is already opened?如果片段已经打开,如何防止从导航抽屉中打开片段,当片段被打开并单击 NavigationDrawer 项目时,片段会再次重新创建,那么如何检查片段是否已经打开?

 public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    if (id == R.id.fragmentA) {
         {   FragmentA fragment = new FragmentA();
             transaction.add(R.id.main_screen, fragment1, "MainFrag").addToBackStack(null);
         }

    } else if (id == R.id.fragmentB) {
        FragmentB fragment = new FragmentB();
        transaction.add(R.id.main_screen, fragment).addToBackStack(null);

    } else if (id == R.id.fragmentC) {
        FragmentC fragment = new FragmentC();
        transaction.replace(R.id.main_screen, fragment).addToBackStack(null);

    }

I solved it in this way:我是这样解决的:

public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    if (id == R.id.n_1 && !(f instanceof MainFragment)) {
        MainFragment fragment = new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    } }

for example I am in the fragment B, And when I click in fragment B again from the NavigationDrawer it recreates, so how to check if the fragment is already shown in the screen or not?例如,我在片段 B 中,当我再次从 NavigationDrawer 中单击片段 B 时,它会重新创建,那么如何检查片段是否已显示在屏幕中?

If you have reference to Fragment B from the NavigationDrawer , you can call mFragmentB.isAdded() to know whether it's already shown or not.如果您从NavigationDrawer引用了 Fragment B,则可以调用mFragmentB.isAdded()以了解它是否已显示。

While adding fragment use tags to replace it.添加片段时使用标签来替换它。

    transaction.replace(R.id.main_screen, fragment, tag);

Now, when menu items are clicked use fragment manager to get the visible fragment using tag :现在,当单击菜单项时,使用片段管理器使用 tag 获取可见片段:

    public boolean isFragmentCurrentlyVisible(String fragmentTag){
    Fragment fragmentToTest;
    fragmentToTest = fragmentManager.findFragmentByTag(fragmentTag);
    if(fragmentToTest!=null && fragmentToTest.isVisible())
        return true;
    return false;

}

Depending on value returned by above method you can add the fragment if not already added.根据上述方法返回的值,您可以添加尚未添加的片段。

I have also struggled with this for a while, I am using navigation components and Kotlin, so finally I got it sorted out.我也为此苦苦挣扎了一段时间,我正在使用导航组件和 Kotlin,所以终于解决了。 It might not be the best solution, but it's simple and works, so I wanted to share it with you:它可能不是最好的解决方案,但它简单且有效,所以我想与您分享:

            when (menuItem.itemId) {

            R.id.nav_board -> {
                if(findNavController(R.id.nav_host_fragment).currentDestination?.label != "listFragment")
                    findNavController(R.id.nav_host_fragment)
                        .navigate(R.id.action_global_listFragment)
            }

The label is the destination label given in the navigation.xml label就是导航给的目的地label.xml

<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/navigation"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="at.example.app.ui.ListFragment"
        android:label="listFragment"
        tools:layout="@layout/fragment_list">
...

So this basically just checks if the current destination (where we're at) is the one that should not be re-opened and only navigates if the current destination is a different one.所以这基本上只是检查当前目的地(我们所在的位置)是否是不应重新打开的目的地,并且仅当当前目的地是另一个目的地时才导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM