无法在Android中调用onConfigurationChanged

pdsfdshx  于 2023-01-11  发布在  Android
关注(0)|答案(1)|浏览(161)
class Rove3LiveVideoFragment : BaseFragment(){

    @Inject
    lateinit var roveR3LiveVideoFragmentViewModel: RoveR3LiveVideoFragmentViewModel

    @Inject
    lateinit var videoControlWidget: VideoControlWidget

    @Inject
    lateinit var notConnectedWidget: NotConnectedWidget

    @Inject
    lateinit var appPreference: AppPreference

    @Inject
    lateinit var videoFullViewWidget: VideoFullViewWidget

    lateinit var customLoader: CustomLoader

    private lateinit var onFullScreenListener: OnFullScreenListener

    var isChangeScreenButtonClicked = false
    var isPortraitMode = true

    override fun onAttach(context: Context) {
        inject(this)
        super.onAttach(context)
        if (activity is Rove3MainActivity) {
            onFullScreenListener = activity as OnFullScreenListener
        } else {
            throw ClassCastException(
                activity.toString()
                        + " must implement MyListFragment.OnItemSelectedListener"
            )
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_rove_r3_live_video, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initWidget(view_live_vide_page)
        customLoader = CustomLoader(requireContext())
        val wifiManager =
            requireContext().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?
        wifiManager?.let {
            if (wifiManager.isWifiEnabled) {
                roveR3LiveVideoFragmentViewModel.apply {
                    customLoader.show(getString(R.string.title_please_wait))
                    if (appPreference.isAppGoesBackGroundExceptHomePage) {
                        appPreference.isAppGoesBackGroundExceptHomePage = false
                        getRove3CameraConnectionStatus(true)
                    } else {
                        getRove3CameraConnectionStatus(false)
                    }
                    observe(stateLiveData, ::getR3ConnectedInLiveVideFragment)
                }
            } else {
                notConnectedView()
            }
        }

    }

    private fun initWidget(view: View) {
        videoControlWidget.apply {
            initView(view)
            addWidget(this)
            activity?.let {
                it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
            }
            observe(onClicked, ::onClickedButton)
        }
        notConnectedWidget.apply {
            initView(view)
            addWidget(this)
        }
        videoFullViewWidget.apply {
            initView(view)
            activity?.let {
                it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
            }
            addWidget(this)
            observe(onClicked, ::onFullVideoViewControllClick)
        }
    }

    private fun onFullVideoViewControllClick(callToAction: VideoFullViewWidget.CallToAction) {
        when (callToAction) {
            is VideoFullViewWidget.CallToAction.PortraitMode -> {
                activity?.let {
                    it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
                }
                showVideoView(false)
                isChangeScreenButtonClicked = true
            }
        }
    }

    private fun onClickedButton(callToAction: VideoControlWidget.CallToAction) {
        when (callToAction) {
            is VideoControlWidget.CallToAction.LandScapeMode -> {
                activity?.let {
                    it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
                }
                isChangeScreenButtonClicked = true
                showVideoView(true)
            }
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("CALLBACKK", "onPause")
    }

    private fun getR3ConnectedInLiveVideFragment(state: BaseViewModel.LiveVideFragmentState) {
        
    }

    private fun notConnectedView() {
        customLoader.hide()
        notConnectedWidget.show()
        view_surface.visibility = View.GONE
        videoControlWidget.hide()
    }

    private fun showLiveVideoWhenR3Connected() {
        notConnectedWidget.hide()
        view_surface.visibility = View.VISIBLE
        videoControlWidget.show()
    }

    private fun addWidget(widget: Widget) {
        lifecycle.addObserver(widget)
    }


    private fun showVideoView(isFullVideoView: Boolean) {
        onFullScreenListener.onFullSreen(isFullVideoView)
        if (isFullVideoView) {
            videoControlWidget.hide()
            videoFullViewWidget.show()
            val params = view_surface.layoutParams as RelativeLayout.LayoutParams
            params.width = ViewGroup.LayoutParams.MATCH_PARENT
            params.height = ViewGroup.LayoutParams.MATCH_PARENT
            view_surface.layoutParams = params

        } else {
            val params = view_surface.layoutParams as RelativeLayout.LayoutParams
            params.width = ViewGroup.LayoutParams.MATCH_PARENT
            params.height =
                (240 * requireContext().applicationContext.resources.displayMetrics.density).toInt()
            view_surface.layoutParams = params
            videoControlWidget.show()
            videoFullViewWidget.hide()
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d("ORIENTAIONNNNNN", "ORIENTATIONCHANGE")
        requireActivity().let {
            if (isChangeScreenButtonClicked) {
                Log.d("ORIENTAIONNNNNN", "BUTTONCLICKED")
                stopAutoOrientationFor3Seconds()
            } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Log.d("ORIENTAIONNNNNN", "LANDSCAPE")
                onFullScreenListener.onFullSreen(false)
                it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
                showVideoView(true)
                isPortraitMode = false
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                Log.d("ORIENTAIONNNNNN", "POTRIAT")
                onFullScreenListener.onFullSreen(true)
                it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
                showVideoView(false)
                isPortraitMode = true
            }
        }
    }

    private fun stopAutoOrientationFor3Seconds() {
        val handler = Handler()
        handler.postDelayed(object : Runnable {
            override fun run() {
                run {
                    isChangeScreenButtonClicked = false
                    activity?.let {
                        it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
                    }
                }
            }
        }, 5000)
    }
   
}

这是我的活动回电

override fun onFullSreen(fullscreen: Boolean) {
            if (fullscreen) {
                nav_view.visibility = GONE
                window.decorView.systemUiVisibility = (android.view.View.SYSTEM_UI_FLAG_IMMERSIVE
                        or android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        or android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
    
                window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                if (supportActionBar != null) {
                    supportActionBar!!.hide()
                }
                Log.d("ORIENTAIONNNNNN", "FULLVIEW")
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            } else {
                nav_view.visibility = VISIBLE
                Log.d("ORIENTAIONNNNNN", "HALFVIEW")
                window.decorView.systemUiVisibility = android.view.View.SYSTEM_UI_FLAG_VISIBLE
                window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                if (supportActionBar != null) {
                    supportActionBar!!.show()
                }
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }
        }

这是我的片段,我有按钮点击全屏,也有按钮点击传感器上的屏幕旋转,它工作正常,我能够旋转屏幕,因为我们不依赖onConfigurationChanged方法,当手动旋转设备时,然后第一次当我启动应用程序并尝试旋转时,然后纵向到横向,然后onConfigurationChanged方法调用,但当我尝试旋转到横向到纵向,然后onConfigurationChanged方法不是打电话,请帮助我,我做错了什么,我遵循mvvm模式。

nukf8bse

nukf8bse1#

对不起,这是我的错误,我得到了这个问题的解决方案。
当我移动it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR这行代码在片段onconfig改变方法结束它的工作很好

相关问题