简体   繁体   English

如何在导航控制器中获取当前显示的片段

[英]How to get the currently displaying fragment in Nav Controller

I use a singe activity and embed multiple fragments into it.我使用单个活动并将多个片段嵌入其中。 Now I would like to know within the single activity class, which Fragment is currently being displayed.现在我想知道在单个活动类中,当前正在显示哪个片段。 How can I do this?我怎样才能做到这一点? I had a look at the solution from here How to know if a Fragment is Visible?我从这里查看了解决方案如何知道片段是否可见?

MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (test != null && test.isVisible()) {
     //DO STUFF
}
else {
    //Whatever
}

But I don't know what to you for the parameter "testID" .但我不知道参数"testID"对你有什么影响。 Further, I tried to implement the solution from Get the current fragment object此外,我尝试从获取当前片段对象中实现解决方案

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);

But here I get the error message: "Cannot resolve symbol 'fragment_container'"但在这里我收到错误消息:“无法解析符号'fragment_container'”

Does anyone have an idea how to get the name of the current Fragment that is being displayed when using a single activity and multiple fragments approach?有谁知道如何获取使用单个活动和多个片段方法时显示的当前片段的名称?

Your 1st approach:您的第一种方法:

If you want to find fragment by tag you need to set the tag first.如果要按标签查找片段,则需要先设置标签。 You do it while making transaction, for ex:您在进行交易时执行此操作,例如:

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, SheetEditorScreen.class, args, "testID" //Here you set the tag
)

and then you will be able to get fragment by tag as you did:然后您将能够像以前一样按标签获取片段:

getSupportFragmentManager().findFragmentByTag("testID");

Your 2nd approach:你的第二种方法:

If you want to get fragment by id you need to pass container's view id which you declare in your activity's layout xml file (set in setContentView(R.id.main_activity) ):如果您想通过 id 获取片段,您需要传递您在活动的布局 xml 文件中声明的容器的视图 id(在setContentView(R.id.main_activity)中设置):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto">

   <androidx.fragment.app.FragmentContainerView
       android:id="@+id/fragment_container" <--- here is the id
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

and then you can find the visible fragment with:然后你可以找到可见的片段:

getSupportFragmentManager().findFragmentById(R.id.fragment_container);

I know there are already 2 answers on mine here but still have 1 more solution so I am writing it here.我知道我的这里已经有 2 个答案,但还有 1 个解决方案,所以我在这里写。 This is the code:这是代码:

NavController navController = Navigation.findNavController(this, R.id.fragment);
        int id=navController.getCurrentDestination().getId();
      if(id==R.id.startGameFragment ){ // change the fragment id
          selectedPosition(0);

      }else if(id==R.id.gameFragment ){ // change the fragment id
          selectedPosition(1);

      }else if(id==R.id.endGameFragment ){ // change the fragment id
          selectedPosition(2);

      }

You can do something like this:你可以这样做:

  1. Create a field of Fragment in your activity.在您的活动中创建一个Fragment字段。 Like this:像这样:
private Fragment mCurrentlyDisplayingFragment;
  1. Whenever changing the fragment, just pass that fragment object to that.每当更改片段时,只需将该片段对象传递给它。 Like this:像这样:
MyFragment fragment = new MyFragment();
// show it in the frame layout
mCurrentlyDisplayingFragment = fragment;

Now, whenever you are checking, just check this field.现在,每当您检查时,只需检查此字段。

Now, because I get clearer about your problem, I am adding another solution.现在,因为我对您的问题更加清楚,我正在添加另一个解决方案。 That worked with FrameLayout and fragment transaction.这适用于FrameLayout和片段事务。 But, this will work with NavController also.但是,这也适用于NavController Try this code:试试这个代码:

public Fragment getCurrentlyVisibleFragment(){
    Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
    return navHostFragment == null ? null : navHostFragment.getChildFragmentManager().getFragments().get(0);
}

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

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