how to use the LifecycleRegistryOwner to keep the broadcast receiver alive only when the activity is alive

591 Views Asked by At

Having some problem to receive the broadcast from third part lib in some case.

(only care the broadcast when the particular activity is alive.)

One use case is (when turn on the ‘Don’t keep activity alive), in the activity it lunches another activity from third part lib.

The current activity would be temporarily destroyed until the new activity is finished (the first activity will be recreated by os).

In the second activity (some third part lib) it does the broadcast. The first activity has registered the receiver, but in its onDestroy() the receiver is unregister, so it missed the broadcast.

Saw the new android arch-lifecycle which could support live data for similar case. Not sure if it could be apply here (the problem might be with the activity context).

This is what I think:

In the activity which extends from LifecycleRegistryOwner, it will observer the activity’s life cycle. as understand the activity could be destroyed and recreated by os (such as at config change, not truly destroyed) but the LifecycleRegistry will tell when is the activity truly destroyed (not the ones that os will recreate it later).

So with help of LifecycleRegistryOwner it could keep some data alive in the lifecycle managed by the LifecycleRegistryOwner for the activity’s true life span.

The question is, if there is a broadcast receiver which would like to be alive with life cycle of the LifecycleRegistryOwner only(the true activity life), is it possible to do the registering the receiver in lifeCycleObser’s onStart() and unregister in it’s onDestroy()? The problem is it needs this activity to register the receiver.

Feel like since the activity could be destroyed and recreated, it should not keep the one activity instance with in the TheLifeCycleObserver which observers the activity’s true life span.

But what is solution if want to the receiver always receiving the broadcast (even the os temporarily destroys the activity and recreate it again shortly) and not registered if this particular activity is truly destroyed?

the classes snippet are listed below:

TheLifeCycleObserver , TheBroadcastReceiver and MainActivity

class TheLifeCycleObserver(private var lifecycle: LifecycleRegistry?, private var lifeCycleChangeListener: OnLifeCycleChange?) : LifecycleObserver {


interface OnLifeCycleChange {
    fun onStart()
    fun onDestroy()
}

init {

}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
    lifeCycleChangeListener!!.onStart()
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {

}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {

}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
    lifeCycleChangeListener!!.onDestroy()
}

}

class MainActivity : AppCompatActivity(), LifecycleRegistryOwner {

    private val mRegistry: LifecycleRegistry = LifecycleRegistry(this);
        override fun getLifecycle() : LifecycleRegistry {
        return mRegistry
    }

    private var theLifeCycleObserver: TheLifeCycleObserve? = null

    fun setupLifeCycleObserver() {

        theLifeCycleObserver = TheLifeCycleObserve(lifecycle, object : TheLifeCycleObserve.OnLifeCycleChange {
            override fun onStart() {

            registerReceiver(this@MainActivity)  //<== can we do it here the pass the activity as context, and the activity could be temporarily destroyed in some time ???

        }

        override fun onDestroy() {

            if (theReceiver != null) {
                [email protected](mASDKBroadcastReceiver);
                theReceiver = null;
            }

            lifecycle.removeObserver(theLifeCycleObserver)
        }

        })

        lifecycle.addObserver(theLifeCycleObserver)
    }

    var theReceiver: TheBroadcastReceiver? = null

    fun registerReceiver(context: Context) {

        if (theReceiver == null) {
            theReceiver = TheBroadcastReceiver(mActivity.getApplicationContext())
        }

        val theIntentFilter = IntentFilter()
        theIntentFilter.addAction(CustomIntent.ACTION_ONE)
        theIntentFilter.addAction(CustomIntent.ACTION_TWO)
        theIntentFilter.addAction(CustomIntent.ACTION_THREE)

        // here the context is the activity, can it live with the TheLifeCycleObserve???

        context.registerReceiver(mASDKBroadcastReceiver, theIntentFilter)

    }

 }



 class TheBroadcastReceiver extends BroadcastReceiver {

        // need the activity's context for some other operation inside the onReceive()

        private final Context mAppContext;

        public TheBroadcastReceiver(@NonNull Context context) {
            mAppContext = context.getApplicationContext();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
               …………
        }
  }
0

There are 0 best solutions below