0

I have declared an activity (RoomActivity) in my Manifest.xml to be able to go in PipMode. Inside that activity i have a RoomFragment which is placed through the NavigationComponent library in the activity.

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" />

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. The Logcat that i see is the following

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!!! Why is this happening??

PendingIntent for the Notification is the following

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!

Can someone help me with this issue?

james04
  • 1,580
  • 2
  • 20
  • 46

1 Answers1

0

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. Specifically, i had to add also the "smallestScreenSize" config

james04
  • 1,580
  • 2
  • 20
  • 46