0

I want to close any dialog open from nav_graph on some event.

NavHostActivity.kt xml

         <fragment
            android:id="@+id/fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_graph_settings" />

nav_graph_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_graph_settings"
    app:startDestination="@id/settingFragment">

    <fragment
        android:id="@+id/settingFragment"
        android:name="matas.matas.settings.fragment.SettingFragment"
        android:label="Min profil”/>
       
</navigation>

on Click of a button in SettingFragment I call below method

private fun highLightMemberCard(view: View) {
        childFragmentManager.let {
            val dialog = MemberCardDialogFragment.getInstance(DisplayUtils.dpToPx(requireContext(),65))
            val ft = it.beginTransaction()
            ft.add(dialog, MemberCardDialogFragment.TAG)
            ft.commitAllowingStateLoss()
        }
    }

I simply want to close MemberCardDialogFragment from Activity on certain event?

Taimoor Khan
  • 541
  • 4
  • 20
  • you can pass in a callback function from activity to fragment, also add the callback function to the parameter of the fragment. If you have not used callback function then add some more code in your question, from where are you instantiating the SettingsFragment – zaid khan Jun 23 '23 at 11:35
  • from nav_graph_setting – Taimoor Khan Jun 23 '23 at 11:49

1 Answers1

0

Create an interface

interface CallbackFunction{
    fun closeDialog()
}

implement that interface in your main activity

class MainActivity : AppCompatActivity(),CallbackFunction{
    private lateinit var navController: NavController


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

      val navHostFragment = supportFragmentManager
    .findFragmentById(R.id.nav_host_fragment) as NavHostFragment

       navController = navHostFragment.navController

   }
    override fun closeDialog() {
          navController.popBackStack()
    }

}

Call the function in your fragment as

(activity as MainActivity).closeDialog()
zaid khan
  • 825
  • 3
  • 11
  • Dialog is opening from SettingFragment . I have to send a callback to Fragment not to activity – Taimoor Khan Jun 23 '23 at 14:07
  • you said "I simply want to close MemberCardDialogFragment from Activity on certain event?" eitherwise, you can implement the interface from SettingsFragment and call the method from settingFragment into dialog fragment – zaid khan Jun 23 '23 at 15:39
  • I have to shut a childFragment of my Nav Graph fragment as I was using old navigation graph in my project that doesn't contain – Taimoor Khan Jun 26 '23 at 05:34