'How to Start Activity with Picture-in-Picture mode directly in Android

I have to open activity directly on picture-in-picture mode like WhatsApp playing video in chats. I tried but it launches activity normally and when I click button and apply the code for entering into picture-in-picture mode only it is working.

I need without clicking any button directly launch the activity on picture-in-picture mode

<activity
            android:name=".PiPActivity"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:label="@string/title_activity_pi_p"
            android:launchMode="singleTask"
            android:resizeableActivity="true"
            android:supportsPictureInPicture="true"
            android:theme="@style/AppTheme.NoActionBar" />

Starting from MainActivity:

startActivity(new Intent(this, PiPActivity.class).putExtra("videoUrl", model.videoUrl));


Solution 1:[1]

As far as I know, an activity can not be opened directly in picture-in-picture (pip) mode. It will be first opened as a normal activity only. However, if you want it to go to pip mode as soon as it is launched, you can call enterPictureInPictureMode(yourParamsToBePassed) inside onCreate() or onStart() or onResume() lifecycle callbacks.

WhatsApp playing video in chats

It does not seem like, WhatsApp uses pip mode to launch the video. It is most probably a custom layout (which looks like a PIP view). This can be proven by the fact that this pip-look-alike-layout is not the topmost view of the screen. If you try moving this pip-look-alike-layout, it will be hidden behind ChatNameBar and TypeMessageBar. Basically, its elevation is lesser than those two. This contradicts the fact that the pip view is shown on top of all the running activities/apps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pulkit garg