Usually if fragment is contained within activity, we can just do this :
supportFragmentManager.beginTransaction()
.add(android.R.id.content, OurFragment())
.commit()
But what about from another fragment though ? they don't even have android.R.id.content
And i know it is not impossible because DialogFragment
exists. The question is how ? is there a way other than overriding dialogfragment ?
I've found out the answer
first you need to add android.permission.SYSTEM_ALERT_WINDOW
on manifest.xml
then on your fragment
class YourFragment : Fragment(){
lateinit var mTopView : View
lateinit var windowManager: WindowManager
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val LAYOUT_FLAG: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT
)
windowManager = requireActivity().applicationContext
.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val li = requireActivity().getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
mTopView = li.inflate(R.layout.yourlayout, null)
windowManager.addView(mTopView, params)
return mTopView
}
override fun onDestroyView() {
super.onDestroyView()
windowManager.removeView(mTopView)
}
}