当一个活动处于另一个活动的画中画模式时,有什么办法可以杀死它吗?

问题描述

我有两个活动 ActivityAActivityB(这个启用了 pip 模式) 当ActivityB处于pip模式时,ActivityA现在出现在前台我想从ActivityA完成/销毁/杀死ActivityB strong> 有没有办法做到这一点?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        val supportsPIP = context!!.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
       
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            if (supportsPIP) {
                enterPictureInPictureMode(mPictureInPictureParamsBuilder!!.build())
            }
        }

    }

解决方法

在检查这个答案 https://stackoverflow.com/a/56896347/13373099 后,我意识到我所要做的只是使用 LocalBroadcastManager 如果有人在执行此操作时遇到问题,这就是我所做的

ActivityB 私有 val mReceiver = 对象:BroadcastReceiver() {

if count == 0:
    print("No values entered therefore no average.")
else:
    avg = total / count
    print(avg)

现在注册监听器

    override fun onReceive(context: Context,intent: Intent?) {
    intent?.let { intent ->
            if (intent.action == "FINISH_ACTIVITY") {
                
                finish(); // finish/kill activity also destroys the pip
                
            }}
    }
    
    

ActivityA

只需发送一个带有意图操作“完成活动”的广播

    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,IntentFilter("FINISH_ACTIVITY));
,

另一种方法是在单例中保存对活动的引用,当你想杀死它时,你调用 finish() 并再次将引用设置为 null。