简体   繁体   English

PictureInPicture模式Activity生命周期问题

[英]PictureInPicture mode Activity lifecycle problem

I have declared an activity (RoomActivity) in my Manifest.xml to be able to go in PipMode.我已经在 Manifest.xml 中声明了一个活动 (RoomActivity),以便能够进入 PipMode。 Inside that activity i have a RoomFragment which is placed through the NavigationComponent library in the activity.在该活动中,我有一个 RoomFragment,它通过活动中的 NavigationComponent 库放置。

room_activity.xml房间活动.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" />

I have a scenario that i put the activity in PipMode, then i close the Pip with X button and then open again from the PendingIntent through the ForegroundService Notification.我有一个场景,我将活动置于 PipMode,然后我用 X 按钮关闭 Pip,然后通过 ForegroundService 通知从 PendingIntent 再次打开。 The Logcat that i see is the following我看到的 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

As you can see, when i close the pip window with the default X button, activity gets destroyed and recreated!!!如您所见,当我使用默认的 X 按钮关闭 pip 窗口时,活动被销毁并重新创建!!! Why is this happening??为什么会这样??

PendingIntent for the Notification is the following通知的 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
            )
        }

The result is that when i finally open the activity from the Notification, i cannot see the UI because the Fragment has not run the onViewCreated method where i have placed my observers!结果是,当我最终从通知中打开活动时,我看不到 UI,因为 Fragment 没有运行我放置观察者的 onViewCreated 方法!

Can someone help me with this issue?有人可以帮我解决这个问题吗?

Fianlly, the problem solved.终于,问题解决了。 The reason why the Activity is recreated after entering PipMode, is because in my manifest i hadn't added all the mandatory ConfigChanges that would prevent the activity to be recreated.进入 PipMode 后重新创建活动的原因是,在我的清单中,我没有添加所有会阻止重新创建活动的强制性 ConfigChanges。 Specifically, i had to add also the "smallestScreenSize" config具体来说,我还必须添加“smallestScreenSize”配置

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

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