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

发布于 2025-01-20 04:01:08 字数 777 浏览 0 评论 0原文

我使用单个活动并将多个片段嵌入其中。现在我想知道在单个活动类中,当前正在显示哪个片段。我该怎么做?我从这里查看了解决方案 如何知道片段是否可见?

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

但我不知道你对参数“testID”有何要求。此外,我尝试实现获取当前片段对象的解决方案

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

但在这里我收到错误消息:“无法解析符号'fragment_container'”

有谁知道如何获取使用单个活动时显示的当前片段的名称和多个片段方法?

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". 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'"

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情域 2025-01-27 04:01:08

我知道这里已经有 2 个答案,但还有 1 个解决方案,所以我将其写在这里。这是代码:

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);

      }

I know there are already 2 answers on mine here but still have 1 more solution so I am writing it here. 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);

      }
一笑百媚生 2025-01-27 04:01:08

您的第一个方法:

如果您想通过标签找到片段,则需要先设置标签。进行交易时,您可以这样做,因为ex:

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

,然后您将能够像您一样通过标签获得片段:

getSupportFragmentManager().findFragmentByTag("testID");

您的第二种方法:

如果您想要通过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>

,您可以找到可见的片段与:

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

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)):

<?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);
遗弃M 2025-01-27 04:01:08

现在,因为我对您的问题更加清楚,所以我添加了另一个解决方案。它与framelayout和片段交易一起使用。但是,这也将与navController一起使用。尝试此代码:

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

Now, because I get clearer about your problem, I am adding another solution. That worked with FrameLayout and fragment transaction. But, this will work with NavController also. Try this code:

public Fragment getCurrentlyVisibleFragment(){
    Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
    return navHostFragment == null ? null : navHostFragment.getChildFragmentManager().getFragments().get(0);
}
み零 2025-01-27 04:01:08

您可以做类似的事情:

  1. 在活动中创建fragment的字段。这样:
private Fragment mCurrentlyDisplayingFragment;
  1. 每当更改片段时,只需将碎片对象传递到其中。像这样:
MyFragment fragment = new MyFragment();
// show it in the frame layout
mCurrentlyDisplayingFragment = fragment;

现在,只要您检查一下,只需检查此字段。

You can do something like this:

  1. Create a field of Fragment in your activity. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文