android - PictureInPicture模式Activity生命周期问题
问题描述
我在 Manifest.xml 中声明了一个活动 (RoomActivity),以便能够进入 PipMode。在该活动中,我有一个 RoomFragment,它通过活动中的 NavigationComponent 库放置。
room_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".ui.activities.room.RoomActivity">
<fragment
android:id="@+id/roomHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/room_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
清单.xml
<activity
android:name=".ui.activities.room.RoomActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|screenSize|fontScale"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:resizeableActivity="true"
android:showOnLockScreen="true"
android:showWhenLocked="true"
android:taskAffinity=".ui.activities.room.RoomActivity"
android:supportsPictureInPicture="true"
android:theme="@style/NoActionBar"
android:windowSoftInputMode="stateAlwaysHidden" />
我有一个场景,我将活动置于 PipMode 中,然后我用 X 按钮关闭 Pip,然后通过 ForegroundService 通知从 PendingIntent 再次打开。我看到的Logcat如下
I: RoomFragment onCreate
I: RoomFragment onViewCreated
I: RoomActivity onCreate
I: RoomFragment onStart
I: RoomActivity onStart
I: RoomActivity onResume
I: RoomFragment onResume
------Entering PipMode------
I: RoomFragment onPause
I: RoomActivity onPause
I: RoomActivity onStop
I: RoomFragment onDestroy
I: RoomActivity onDestroy
I: RoomFragment onCreate
I: RoomFragment onViewCreated
I: RoomActivity onCreate
I: RoomFragment onStart
I: RoomActivity onStart
I: RoomActivity onResume
I: RoomFragment onResume
I: RoomFragment onPause
I: RoomActivity onPause
------Kill pip with the X button------
I: RoomActivity onStop
I: RoomFragment onDestroy
I: RoomActivity onDestroy
I: RoomFragment onCreate
I: RoomFragment onViewCreated
I: RoomActivity onCreate
I: RoomFragment onStart
I: RoomActivity onStart
I: RoomActivity onResume
I: RoomFragment onResume
I: RoomFragment onPause
I: RoomActivity onPause
I: RoomActivity onStop
------Open with Notification------
I: RoomFragment onStart
I: RoomActivity onStart
I: RoomActivity onResume
I: RoomFragment onResume
如您所见,当我使用默认的 X 按钮关闭 pip 窗口时,活动被破坏并重新创建!!!为什么会这样??
通知的 PendingIntent 如下
private fun getPendingIntent(bundle: Bundle): PendingIntent =
Intent(this@WebRtcVideoService, RoomActivity::class.java).let {
it.putExtras(bundle)
it.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
PendingIntent.getActivity(this@WebRtcVideoService,0, it,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or 0 else 0
)
}
结果是,当我最终从通知中打开活动时,我看不到 UI,因为 Fragment 没有运行我放置观察者的 onViewCreated 方法!
有人可以帮我解决这个问题吗?
解决方案
终于,问题解决了。进入 PipMode 后重新创建 Activity 的原因是,在我的清单中,我没有添加所有会阻止重新创建活动的强制 ConfigChanges。具体来说,我还必须添加“smallestScreenSize”配置
推荐阅读
- javascript - 幻灯片更改后暂停 html 视频,光滑的轮播
- google-cloud-platform - 我可以将我的 PC 直接连接到 Google Cloud VPN
- javascript - 为以下 Javascript 对象创建嵌套对象?
- java - 如何在 Java 中外部化列表
- python - 松弛机器人 | 检查消息反应
- sql - SQL将一行转换为具有未知数量的不同值的列
- c# - 有没有办法获取我可以在通用方法中使用的类型列表?
- android - 适用于 Android 设备的 Linux Ubuntu 串行 Com 端口
- javascript - 目标 _blank 不适用于下载属性
- python - 在选择数据框列名称时为其提供别名