PictureInPicture模式活动生命周期问题



我在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>

manifest.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窗口时,活动会被破坏并重新创建!!!为什么会发生这种情况??

通知的未决意向如下

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
)
}

结果是,当我最终从Notification打开活动时,我看不到UI,因为Fragment没有运行我放置观察者的onViewCreated方法!

有人能帮我解决这个问题吗?

最后,问题解决了。之所以在进入PipMode后重新创建活动,是因为在我的清单中,我没有添加所有阻止重新创建活动的强制性ConfigChanges。具体地说,我还必须加上";smallestScreenSize";配置

最新更新