59

I tried to find a method in the new Navigation components but I didn't find anything about that.

I have the current destination with :

mainHostFragment.findNavController().currentDestination

But I can't get any reference to the displayed fragment.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
BenjaminBihr
  • 1,252
  • 2
  • 10
  • 15

10 Answers10

61

Reference to the displayed fragment (AndroidX):

java

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

kotlin

val navHostFragment: Fragment? =
        supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
navHostFragment?.childFragmentManager?.fragments?.get(0)

Here nav_host_fragment is an ID of the fragment tag in your activity_main.xml with android:name="androidx.navigation.fragment.NavHostFragment"

Muhamed El-Banna
  • 593
  • 7
  • 21
isabsent
  • 3,683
  • 3
  • 25
  • 46
50

Navigation does not provide any mechanism for getting the implementation (i.e., the Fragment itself) of the current destination.

As per the Creating event callbacks to the activity, you should either communicate with your Fragment by

  • Having the Fragment register a callback in its onAttach method, casting your Activity to an instance of an interface you provide
  • Use a shared ViewModel that your Activity and Fragment use to communicate.
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 8
    All links show how to communicate between fragments. But how we can call fragment method from activity if navigation does not provide any mechanism for getting the implementation of the current destination? – Sever Apr 17 '19 at 18:00
  • 8
    @Sever - see [this issue](https://issuetracker.google.com/issues/119800853) and how to use `getPrimaryNavigationFragment()` to retrieve the current Fragment. – ianhanniballake Apr 17 '19 at 18:03
  • 5
    Big thanks. It works. val navHostFragment = supportFragmentManager.primaryNavigationFragment as NavHostFragment val yourFragment = currentFragmentClassName.childFragmentManager.primaryNavigationFragment as YourFragment – Sever Apr 17 '19 at 18:25
  • @ianhanniballake: Sorry, I didn't understand is there a way to get the foreground fragment by means of `getPrimaryNavigationFragment()` now or they are going to provide such opportunity in the future only!? – isabsent Dec 11 '19 at 06:17
  • 1
    Well, to share ViewModel between two fragments, one of them must be its owner. And the other one must find that owner in order to fetch that ViewModel. So… just never use Navigation, it's retarded. – Miha_x64 Apr 18 '21 at 17:18
  • It is incomprehensible why Navigation does not maintain a list of fragments, for example needed when MainActivity wants to call a method in a child fragment. In consequence, you need to use the "good old ways" for that, and then, whats the point of introducing a Navigation system? I don@t get it. – carl Apr 21 '21 at 09:39
30

You can do something like this:

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

But for more advanced communication Listeners with callback methods registered in Fragment.onAttach() (Fragment -> Activity rather one direction communication) and SharedViewModel (bidirectional, important to have ViewModelProviders, and Lifecycle owner that is scoped to getActivity() rather)

Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • You can remove `as? NavHostFragment` and make the if-statement `if (navHostFragment is NavHostFragment)` – Shalbert Dec 01 '21 at 19:43
14

Based on other answers

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

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

worked for me

Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35
9

Using Michal's answer I wrote this extension function for testing:

@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
}

Used like this:

val myFragment = activity.getFragment(MyFragment::class.java)
CampbellMG
  • 2,090
  • 1
  • 18
  • 27
  • 2
    You can squish this down to `inline fun FragmentManager.findFragmentByClass(): T? = fragments.firstOrNull { it is T } as T?` – drdaanger Nov 08 '19 at 21:40
5

You can do like this:

@Override
public void 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);
    }
}

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);
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Soumen Das
  • 1,292
  • 17
  • 12
3

I post my complete answer with androidx. Care : in my case i needed to retrieve one of the child fragment (could not be the first).

In MainActivity you should have something like :

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

Then you have to create a method to retrieve the good fragment.

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

Finally you get it.

Zhar
  • 3,330
  • 2
  • 24
  • 25
2

From an Activity which has NavHostFragment, below code snippet can be used to retrieve the instance of the Active Fragment.

kotlin.

val currentFragment = mNavHostFragment?.childFragmentManager?.primaryNavigationFragment()

java

Fragment navHostFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
Fragment currentFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
Beto
  • 806
  • 3
  • 12
  • 33
burak isik
  • 395
  • 5
  • 6
0

Really the best and correct way to handle this is to use an interface. View models should really be used for passing data between the activity and fragments. Here is how I solved this problem:

Create interface

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

Make sure activity implements interface

class NotesActivity : AppCompatActivity(), NavigationInterface {}

Make sure to create lateinit var for each fragment you need to reference and then

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

Now in each fragment in the onCreateView method, make sure to create the interface listener and passback the fragment with the interface

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
}

Now back in the activity, you should be able to instantiate your fragment objects for reference via interface callback

override fun referenceCourseListFragment(fragment: CourseListFragment) {
    courseListFragment = fragment
}

override fun referenceCouseDetailFragment(fragment: CourseDetailInfoFragment) 
{
    courseDetailInfoFragment = fragment
}
Droid Chris
  • 3,455
  • 28
  • 31
0

I think there is chance of exceptions, so you can use interface callback this is solid way to communicate from one end to other end only pass reference check below code.

Fragment A(){ interface ClickPostItemListener { fun onClicked(position: Int) }

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

}  } Fragment B(clickItem:ClickPostItemListener ){ clickItem() }
ShehryarAhmed
  • 95
  • 1
  • 11