Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>At this point I am just thinking about making an inner class <code>BroadcastReceiver</code> in each activity and putting the desired functionality in those</p> </blockquote> <p>Yep, this is the best way to implement that feature. If you want to make the functionality common rather than copying it into each Activity, you could create a base class for each Activity in the application to extend if you want the <code>BroadcastReceiver</code> to be an inner class.</p> <p>You can also still define it in a separate file as you likely already have, but just register the receiver directly in each Activity rather than once in the AndroidManifest.xml file. If you register/unregister the receiver in <code>onResume()</code>/<code>onPause()</code>, then you will still only have one active receiver at any given time which is attached to the currently foreground Activity. <strong>These methods also keep you from receiving the given broadcast when your app isn't in the foreground at all, whereas a receiver defined in the manifest is always active, forcing you to handle that case.</strong></p> <p>If you need to explicitly pass some identifier to each instance of the <code>BroadcastReceiver</code> as to which Activity it's associated with, define a simple identifier (like the class name as a String) that you can check in the receiver. It's not a good idea to pass the Activity itself, for similar reasons as to why the framework doesn't do that either. If you're curious, I wrote a <a href="http://www.doubleencore.com/2013/06/context/" rel="nofollow">blog post</a> not long ago detailing some of the issues with passing around Context like that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload