I'm using navigation to navigate between fragments.
And I have a DialogFragment, it can called from from many fragment like this:
val dialog = FragmentDialog
dialog.show(childFragmentManager, "home_fragment")
And I want to know dialogfragment called by which fragment
I tried with FragmentManager.backStackEntryCount but it's seem doesn't work
Can I have a advice for this problem ???
CodePudding user response:
There are multiple ways to do this ..
You can just Pass a key in
Bundle
to yourDialogFragment
to check which Fragment opened it ..If your Dialog fragment a attached to a
fragment
in each case you can just usegetParentFragment()
.getParentFragment()
will return the instance of that fragment. If its attached to activity thengetParentFragment()
will be null so watch out for this case also .you can also use
getTag()
from where it was open byt passing different tags to transaction .
CodePudding user response:
can you explain why you need to know dialogfragment called by which fragment
fun showInAppDialog() {
val mInAppFragment: InAppScreenFragmentDialog =
InAppScreenFragmentDialog.instance
supportFragmentManager.beginTransaction().add(mInAppFragment,
"IN_APP_DIALOG")
.commitAllowingStateLoss()
}
private fun dismissInAppDialog() {
if (isFinishing) return
val manager: FragmentManager = supportFragmentManager
val mInAppFragment: InAppScreenFragmentDialog? =
manager.findFragmentByTag("IN_APP_DIALOG") as InAppScreenFragmentDialog?
if (mInAppFragment!= null) {
supportFragmentManager.beginTransaction().remove(mInAppFragment).commitAllowingStateLoss()
}
}