0

I have two Activities ActivityA and ActivityB (this one has pip mode enabled) when ActivityB is in pip mode,ActivityA comes to the foreground now I want to finish/destroy/kill ActivityB from ActivityA is there any way to do this?

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())
            }
        }

    }
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Sarath Siva
  • 547
  • 3
  • 14

2 Answers2

3

After checking this answer https://stackoverflow.com/a/56896347/13373099 I realized that All I had to do was just use LocalBroadcastManager if anyone having trouble implementing this, here is what I did

in ActivityB

private val mReceiver = object : BroadcastReceiver() {

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

now register the listener

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

in ActivityA

just send a broadcast with intent action "FINISH ACTIVITY"

 val intent = Intent("FINISH_ACTIVITY")
 LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55
Sarath Siva
  • 547
  • 3
  • 14
1

Another way it's to save the reference to the activity in a singleton and when you want to kill it you call finish() and the set the reference to null again.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44