Last active
November 22, 2021 10:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Reference: https://proandroiddev.com/task-management-for-picture-in-picture-mode-on-android-o-882103271cad