簡體   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