Reputation: 15042
I noticed that while an Activity is in picture-in-picture (PIP) mode in Android it does not appear to receive results were requested via startActivityForResult(...)
. Unless I am doing something wrong? I wrote a bare bones sample app that goes into PIP mode and then starts an activity for result which calls setResult(...)
and finish
so I don't think I could have messed this up.
I am assuming this bug is probably an unintended side-effect of the differences in Activity lifecycle for activities that are running in PIP mode; they seem to be in the "started but paused" state most of the time and not resumed.
Maybe someone has a workaround to get the result some other way? Although I guess reflection isn't possible now that Android P is restricted non-SDK interfaces. It seems the best we can do is invent another mechanism to send results which uses broadcasts but that wouldn't work with existing Activities that are built-in such as getting results from Intent.ACTION_GET_CONTENT
.
Upvotes: 2
Views: 1959
Reputation: 36
I guess that's because when the activity enters PIP mode, it will be put into a new task with the flag FLAG_ACTIVITY_NEW_TASK.
According to the official docs, this flag doesn't allow us to request a result from the caller activity.
This flag can not be used when the caller is requesting a result from the activity being launched.
To solve this issue, maybe we can use other libraries like eventBus to send the activity finish() event and data.
Upvotes: 0
Reputation: 11
Following this post https://proandroiddev.com/task-management-for-picture-in-picture-mode-on-android-o-882103271cad , we override the Activity method finish()
to check when the PIP mode was launched, if so, we navigate to the launcher activity before calling the super class method for the the activity result logic to work.
This is the code to navigate to the launcher activity
override fun finish() {
if (pictureInPictureLaunched) navToLauncherTask()
super.finish()
}
fun navToLauncherTask() {
val activityManager = baseContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
// iterate app tasks available and navigate to launcher task (browse task)
val appTasks = activityManager.appTasks
for (task in appTasks) {
val baseIntent = task.taskInfo.baseIntent
val categories = baseIntent.categories
if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
task.moveToFront()
return
}
}
}
Upvotes: 1