0

My launch Activity is A. In A, I have a BroacastReceiver that I want to unregister only if the application (and not only the activity) is not visible anymore. So, if I launch activity B, A still gets updates from the recevier.

I'm using Cornstalks's answer here and I'm able to unregister the receiver only if the app is not visible anymore:

in A:

@Override
protected void onStop() {
    super.onStop();

    if (!LifecycleHandler.isApplicationVisible() && dataUpdateReceiver != null)
        unregisterReceiver(
}

BUT this works only if the last activity is A. If the user leaves the application from B, the receiver is still registered.

So how can I achieve this?

Community
  • 1
  • 1
andrew
  • 3,879
  • 4
  • 25
  • 43
  • Where do you register your broadcastReceiver ? And what do you do in your onReceive() method ? – Elodie E2 Feb 22 '15 at 21:35
  • in my onResume() method. In onReceive I update an ArrayList and a couple of variables... – andrew Feb 22 '15 at 21:38
  • If you register your broadcastReciever in the onResume method, you should unregister it in the onPause() method. (And if you register your BR in the onStart(), you unregister it in the onStop method). But that doesn't solve your problem. What I would do is register and unregister the BR in the application class. I am going to post an example. The thing is i am not sure that the ArrayList and a couple of variables you want to update will be accessible from there. Do you think you can refactor your code in order to make it possible? – Elodie E2 Feb 22 '15 at 21:42
  • Sorry. I just remembered that the onTerminate() method is called when the application is stopped, but not when it's in background so the only solution I see is to create a BaseActivity class which counts how many Activity you starts and unregister the BR only when you are closing the last one – Elodie E2 Feb 22 '15 at 21:53

0 Answers0