将 android 之前活动中的 activity 最小化

Minimizimg an activity in previous activities in android

我正在开发一个支持即时消息的视频通话组件。我想在消息 activity 中添加按下后退时最小化视频通话屏幕的选项和 return 以同时执行这两项操作(类似 WhatsApp 的功能)。问题是,我找不到解决方案,因为返回到上一个 activity 将完成当前的。那么我如何 override 我的 onBackpressed() 才能最小化而不是完成通话屏幕 activity (假设将其添加到通知)和 return 到消息传递 activity?

你想做的事情叫做Picture-in-Picture

Android 8.0 (API level 26) allows activities to launch in picture-in-picture (PIP) mode. PIP is a special type of multi-window mode mostly used for video playback. It lets the user watch a video in a small window pinned to a corner of the screen while navigating between apps or browsing content on the main screen.

一个非常简单的例子:

1) 在您的 AndroidManifest 文件中指定 activity 支持 PIP:

<activity
      android:name=".VideoActivity"
      android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
      android:resizeableActivity="true"
      android:supportsPictureInPicture="true"/>

2) 触发 PIP 模式以响应某些用户操作,例如 onBackPressed():

override fun onBackPressed() {
   // Ideally you would want to enter PIP while a video is playing, so:
   val isVideoPlaying = // have a condition here that holds true while the video is playing

   if (isVideoPlaying) {
        enterPictureInPictureMode()
   } else {
        super.onBackPressed()
   }
}