androidandroid-layoutpicture-in-pictureandroid-picture-in-picture

Picture in Picture Opens new window of the app and shows tons of recent tasks


I am playing my videos in PIP mode when on Pressback button. Everything is fine. video playing perfectly. navigation etc. But problem is when i Close PIP mode. There is new window created on my recent apps window. IMAGE. See those white windows those are left by pip mode when i click pip mode close button.

Is there any way to solve this and close activity when I close the pip mode please help..


Solution

  • Try to use below configuration in Manifest for your Activity to avoid multiple instances

    <activity
          android:name=".view.activities.PlayerActivity"
          android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|uiMode"
          android:excludeFromRecents="true"
          android:launchMode="singleTask"
          android:resizeableActivity="false"
          android:taskAffinity=".PlayerActivity"
          android:supportsPictureInPicture="true"
          android:windowSoftInputMode="adjustResize"
          tools:targetApi="n"/>
    

    And below approach to finish the activity when closed in PIP mode

    var isInPipMode: Boolean = false
    
    override fun onPictureInPictureModeChanged(
            isInPictureInPictureMode: Boolean,
            newConfig: Configuration?
        ) {
            super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
            isInPipMode = isInPictureInPictureMode
        }
    
     override fun onStop() {
            super.onStop()
            if(isInPipMode){
                finish()
            }
        }