繁体   English   中英

PictureInPicture模式Activity生命周期问题

[英]PictureInPicture mode Activity lifecycle problem

我已经在 Manifest.xml 中声明了一个活动 (RoomActivity),以便能够进入 PipMode。 在该活动中,我有一个 RoomFragment,它通过活动中的 NavigationComponent 库放置。

房间活动.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>

清单文件

<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 后重新创建活动的原因是,在我的清单中,我没有添加所有会阻止重新创建活动的强制性 ConfigChanges。 具体来说,我还必须添加“smallestScreenSize”配置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM