Kill Child Activity from parent

Viewed 46

A. B. C |. |. | MainActivity Bluetooth Routines with CallBack

MainActivity Serves 2 purposes for me:

  1. Manages Bluetooth BLE system calls, including a “CallBack” function that the system calls when a scan is started with: bleScanner.startScan(filters, scanSettings, scanCallback)
  2. Serves as “traffic cop” or “controller” for which activity the user sees, either A, B, or C.

A, B, C is always started from MainActivity with something like:

    startActivityForResult <Act A> (1)

A, B, C always finishes with something like:

setResult(9996, intent) finish()

And A, B, C iare never resident at the same Time.

So, suppose A is active, and a TextView is filled with data obtained in CallBack and needs to be refreshed whenever new data comes in. All good moving in and out of A, B, C UNTIL a CallBack occurs. The system immediately jumps to the CallBack method in MainActivity with No normal lifecycle calls (onPause, onExit, etc..) and A is left on the stack.

I would like to have a way to kill A from MainActivity after the CallBack and start over clean. I have tried various things such as:

((Activity) Class.forName(className).newInstance()).finish();

Trying to get top with:

        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val taskInfo = am.getRunningTasks(1)
        val componentInfo = taskInfo[0].topActivity
        val Actvity_Name = taskInfo[0].topActivity?.className
        val PKG_Name = componentInfo?.packageName

And then tried killing Activity_Name with various techniques unsuccfully.

I would appreciate help with this issue.

1 Answers
  1. Check to see if onNewIntent works for you:

https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)

The thing is that the activity should be a specific type. Also you need to know which one is active to call it.

  1. So maybe you can do it with a BroadcastReceiver registered in the A,B,C Activities. You can send the broadcast from the MainActivity and whatever activity on top of it is active it can finishes itself:

https://developer.android.com/guide/components/broadcasts

Related