我如何在NavHostFragment中检索当前片段?

时间:2018-06-04 21:46:33

标签: android android-fragments kotlin android-architecture-components android-architecture-navigation

我试图在新的导航组件中找到一个方法,但我没有找到任何相关信息。

我目前的目的地是:

mainHostFragment.findNavController().currentDestination

但我无法参考显示的片段。

10 个答案:

答案 0 :(得分:9)

导航不提供任何获取当前目的地实现的机制(即片段本身)。

根据Creating event callbacks to the activity,你应该通过

与你的片段进行交流
  • 让Fragment在其onAttach方法中注册回调,将您的Activity转换为您提供的接口的实例
  • 您的活动和片段用于沟通的
  • Use a shared ViewModel

答案 1 :(得分:9)

您可以执行以下操作:

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        val navHostFragment = supportFragmentManager.fragments.first() as? NavHostFragment
        if(navHostFragment != null) {
            val childFragments = navHostFragment.childFragmentManager.fragments
            childFragments.forEach { fragment ->
                fragment.onActivityResult(requestCode, resultCode, data)
            }
        }
    }

但要进行更高级的通信, Fragment.onAttach()Fragment -> Activity rather one direction communication)和 SharedViewModel bidirectional,对于拥有ViewModelProviders, and Lifecycle owner that is scoped to getActivity()来说很重要)

答案 2 :(得分:2)

@Override 公共无效onBackPressed(){ super.onBackPressed();

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

  }else if(id==R.id.gameFragment ){
      selectedPosition(1);

  }else if(id==R.id.endGameFragment ){
      selectedPosition(2);

  }

}enter code here

private void selectedPosition(int pos){
    for (int i = 0; i >=nav_view.getMenu().size(); i++) {
        nav_view.getMenu().getItem(pos).setChecked(false);

    }
    nav_view.getMenu().getItem(pos).setChecked(true);
}

答案 3 :(得分:1)

从具有NavHostFragment的Activity中,下面的代码片段可用于检索Active Fragment的实例。

kotlin

val currentFragment = mNavHostFragment?.childFragmentManager?.primaryNavigationFragment

java

Fragment navHostFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
Fragment currentFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);

答案 4 :(得分:0)

使用Michal的答案,我编写了此扩展功能进行测试:

@Suppress("UNCHECKED_CAST")
fun <F : Fragment> AppCompatActivity.getFragment(fragmentClass: Class<F>): F? {
    val navHostFragment = this.supportFragmentManager.fragments.first() as NavHostFragment

    navHostFragment.childFragmentManager.fragments.forEach {
        if (fragmentClass.isAssignableFrom(it.javaClass)) {
            return it as F
        }
    }

    return null
}

像这样使用:

val myFragment = activity.getFragment(MyFragment::class.java)

答案 5 :(得分:0)

参考显示的片段(AndroidX):

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

nav_host_fragment是一个ID  fragment中的activity_main.xml标签中的android:name="androidx.navigation.fragment.NavHostFragment"标签中的一个

答案 6 :(得分:0)

基于其他答案

Fragment navHostFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
Fragment fragment = navHostFragment.getChildFragmentManager().getFragments().get(0);

((Your Fragment Class) fragment).(public method inside the fragment)

为我工作

答案 7 :(得分:0)

我用androidx发布了完整的答案。 护理:在我的情况下,我需要检索一个子片段(不能是第一个)。

MainActivity 中,您应该有类似的内容:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                 R.id.mytest, R.id.nav_help)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
...

然后,您必须创建一种方法来检索好的片段。

 private MyTestFragment getMyTestFragment(){
        MyTestFragment resultFragment = null;
        Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        if(navHostFragment != null && navHostFragment.getChildFragmentManager() != null) {
            List<Fragment> fragmentList = navHostFragment.getChildFragmentManager().getFragments();
            for (Fragment fragment : fragmentList) {
                if (fragment instanceof MyTest) {
                    resultFragment = (MyTest) fragment;
                    break;
                }
            }
        }
        return resultFragment;
    }

最后,你明白了。

答案 8 :(得分:0)

真的来处理这个最好的和正确的方式是使用一个接口。视图模型应该真正用于在活动和片段之间传递数据。这是我如何解决了这个问题:

创建接口

interface NavigationInterface {
    fun referenceCourseListFragment(fragment: CourseListFragment)
    fun referenceCouseDetailFragment(fragment: CourseDetailInfoFragment)
}

确认活动工具接口

class NotesActivity : AppCompatActivity(), NavigationInterface {}

请一定要为每个需要引用,然后片段lateinit VAR

private lateinit var courseListFragment: CourseListFragment
private lateinit var courseDetailInfoFragment: CourseDetailInfoFragment

现在在onCreateView方法中的每个片段,以确保创建接口侦听和与接口回传的片段

private lateinit var navigationInterface: NavigationInterface

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    //establish interface communication
    activity?.let {
        instantiateNavigationInterface(it)
    }
    // Inflate the layout for this fragment
    fragmentView = inflater.inflate(R.layout.fragment_course_list, container, 
    false)
    navigationInterface.referenceCourseListFragment(this)
    return fragmentView
}

现在回到活动中,你应该能够通过接口回调

来实例片段对象以供参考
override fun referenceCourseListFragment(fragment: CourseListFragment) {
    courseListFragment = fragment
}

override fun referenceCouseDetailFragment(fragment: CourseDetailInfoFragment) 
{
    courseDetailInfoFragment = fragment
}

答案 9 :(得分:0)

我认为存在异常的可能性,因此您可以使用接口回调,这是一种从一端到另一端进行通信的可靠方式,仅通过代码下方的引用检查。

<块引用>

Fragment A(){ interface ClickPostItemListener { 有趣的点击(位置:Int) }

val itemListener = object : ClickPostItemListener{
    override fun onClicked(position: Int) {
        postBackNForth(position)
    }

}  } Fragment B(clickItem:ClickPostItemListener ){ clickItem() }