Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Last active November 22, 2021 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhimuktheeswarar/3505c676736ef2163f227ce2c18b703a to your computer and use it in GitHub Desktop.
Save abhimuktheeswarar/3505c676736ef2163f227ce2c18b703a to your computer and use it in GitHub Desktop.
Logic to support back navigation to parent activity properly when returning from PiP mode
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourapp">
<dist:module
dist:instant="false"
dist:title="@string/feature_title">
<dist:fusing dist:include="true" />
<dist:delivery>
<dist:on-demand />
</dist:delivery>
</dist:module>
<application>
<activity
android:name="com.app.PipActivity"
android:taskAffinity="com.app.something"
android:screenOrientation="portrait"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:supportsPictureInPicture="true"
tools:ignore="LockedOrientationActivity"
tools:targetApi="n" />
</application>
</manifest>
class PipActivity : Activity() {
var isActivityBackStackLost = false
var isInPictureInPictureMode = false
object {
fun open(activity: Activity) = with(activity) {
val intent = Intent(this, Class.forName("com.app.PipActivity"))
//Add this, if the activity is part of a dynamic module.
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
}
override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration?) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
isActivityBackStackLost = true
isInPictureInPictureMode = isInPictureInPictureMode
}
override fun finish() {
if (!isInPictureInPictureMode && isActivityBackStackLost) {
finishAndRemoveTask()
val activityManager: ActivityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
activityManager.appTasks.forEach { task ->
val baseIntent: Intent = task.taskInfo.baseIntent
val categories = baseIntent.categories
if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
task.moveToFront()
return
}
}
} else {
super.finish()
}
}
}
Object TaskManager {
fun navigateToLauncherTask(applicationContext: Context) {
val activityManager: ActivityManager = applicationContext.getSystemService(ACTIVITY_SERVICE) as ActivityManager
// iterate app tasks available and navigate to launcher task (browse task)
activityManager.appTasks.forEach { task ->
val baseIntent: Intent = task.taskInfo.baseIntent
val categories = baseIntent.categories
if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
task.moveToFront()
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment