Setting the orientation for only 1 fragment in my activity while the rest is in portrait

后端 未结 8 1094
难免孤独
难免孤独 2020-12-05 01:52

My app needs to be in portrait mode so I set it in the manifest by:

android:screenOrientation=\"portrait\"

But I just recently added anothe

8条回答
  •  Happy的楠姐
    2020-12-05 02:12

    ok after i almost blew my head off this worked for me with jetpack navigation components fragments

    so this is the base fragment class

    abstract class BaseFragment : Fragment(){   
    
    var rotated = false
    
    fun rotate() {
            val currentOrientation = activity?.resources?.configuration?.orientation //this is diffrent from  Configuration.ORIENTATION_LANDSCAPE
            Log.e("currentOrientation--->",currentOrientation.toString())
    
            if (currentOrientation != null) {
                if (currentOrientation !=  Configuration.ORIENTATION_LANDSCAPE) {
                    activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                }else{
                    rotated=true
                }
            }else{
                //impossible
                activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            }
        }
    
    
        override fun onDestroyView() {
            super.onDestroyView()
            if (rotated)
                activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
        }
    
    }
    

    and i put this in any fragment i wish it to be landscaped

    override fun onStart() {
        super.onStart()
        rotate()
    }
    

提交回复
热议问题