In the process of porting an iPhone application over to android, I am looking for the best way to communicate within the app. Intents seem to be the way to go, is this the best (only) option? NSUserDefaults seems much lighter weight than Intents do in both performance and coding.
I should also add I have an application subclass for state, but I need to make another activity aware of an event.
Here is something similar to @Shiki answer, but from the angle of iOS developers and Notification center.
First create some kind of NotificationCenter service:
Then, you will also need some enum type to be secure of mistakes in coding with strings - (NotificationType):
Here is usage(add/remove observers) for example in activities:
and here is finally how we post notification to NotificationCenter from some callback or rest service or whatever:
that's it, cheers!
You could use this: http://developer.android.com/reference/android/content/BroadcastReceiver.html, which gives a similar behavior.
You can register receivers programmatically through Context.registerReceiver(BroadcastReceiver, IntentFilter) and it will capture intents sent through Context.sendBroadcast(Intent).
Note, though, that a receiver will not get notifications if its activity (context) has been paused.
I found that the usage of EventBus of Guava lib is the simplest way for publish-subscribe-style communication between components without requiring the components to explicitly register with one another
see their sample on https://code.google.com/p/guava-libraries/wiki/EventBusExplained
you can add this lib simply on Android Studio by adding a dependency to your build.gradle:
The best equivalent I found is LocalBroadcastManager which is part of the Android Support Package.
From the LocalBroadcastManager documentation:
When using this, you can say that an
Intent
is an equivalent to anNSNotification
. Here is an example:ReceiverActivity.java
An activity that watches for notifications for the event named
"custom-event-name"
.SenderActivity.java
The second activity that sends/broadcasts notifications.
With the code above, every time the button
R.id.button_send
is clicked, an Intent is broadcasted and is received bymMessageReceiver
inReceiverActivity
.The debug output should look like this:
You could try this: http://developer.android.com/reference/java/util/Observer.html
You could use weak references.
This way you could manage the memory yourself and add and remove observers as you please.
When you addObserver add these parameters - cast that context from the activity you are adding it in to the empty interface, add a notification name, and call the method to run interface.
The method to run interface would have a function that is called run to return the data that you are passing something like this
Create a observation class that invokes a reference with a empty interface. Also construct your Themethodtorun interface from the context being passed in the addobserver.
Add the observation to a data structure.
To call it would be the same method however all you need to do is find the specific notification name in the data structure, use the Themethodtorun.run(notification_name, data).
This will send a callback to where ever you created an observer with a specific notification name. Dont forget to remove them when your done!
This is good reference for weak references.
http://learningviacode.blogspot.co.nz/2014/02/weak-references-in-java.html
I am in the process of uploading this code to github. Keep eyes open!