DialogFragment issues with screen orientation and MediaController

蓝咒 提交于 2019-12-11 19:19:24

问题


I have an app which plays videos in a DialogFragment. I have added MediaController to the VideoView however there are two issues:

  • MediaController is hidden behind the DialogFragment.
  • Change in screen orientation when the DialogFragment is visible results in an exception that the activity has a leaked window
  • For the first one, I tried using linearLayoutParent.bringChildToFront(mediaControls) which did not work. And I do not know how to deal with the second one.

    Help me out. :)


    回答1:


    Although this is an old question, I was also displaying a VideoView in a DialogFragment and had the same issue where the MediaController was hidden behind the DialogFragment.

    For anyone that is also looking at doing the same, this is what I did.

    //Remove the mediaController from it's parent view.
    ((ViewGroup) mediaController.getParent()).removeView(mediaController);
    //Add the mediaController to a FrameLayout within your DialogFragment.
    ((FrameLayout) findViewById(R.id.controlsWrapper)).addView(mediaController);
    

    Just make sure your FrameLayout fills the width of the screen, and set the gravity to the bottom of the screen.

    Also note that tapping the VideoView will not show and hide the MediaController (for some reason).

    So you'll need to add a View.onClickListener to the VideoView to add and remove the MediaController.




    回答2:


    To your second point:

    Try setRetainInstance(true) in your onCreate of the Fragment. With this the Fragment will survive while the Activity gets destroyed, like on a configuration change. If you are using the support library you will need this to really prevent the closing of your Fragment:

     @Override
     public void onDestroyView() {
         if (getDialog() != null && getRetainInstance())
             getDialog().setDismissMessage(null);
             super.onDestroyView();
     }
    

    This is needed to overwrite the dismiss listener of the DialogFragment.

    If you have calls on the Activity from the Fragment update the context / callback accordingly to keep track of the reference to the new created Activity. Therefor you can use onAttach() or onActivityCreated().



    来源:https://stackoverflow.com/questions/18163520/dialogfragment-issues-with-screen-orientation-and-mediacontroller

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!