Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the Application communicate with the current Activity

My Android app uses its Application class (to be precise its Application subclass) to run a thread to retrieve data from a website. Once it does it successfully it is supposed to give this data to the running Activity (there are 2 activities in my app). How could I notify the Activity about this? How can I tell the current Activity that there is new available data that should be retrieved and presented? Thanks for your answers.

like image 604
user1012480 Avatar asked Sep 11 '26 18:09

user1012480


1 Answers

What about using a broadcast system.

You can get the context in the Application class by using getApplicationContext(); Then you can send your own custom broadcast:

Intent i = new Intent();
i.setAction("your.package.customintent.TEST");
context.sendBroadcast(i);

Then you can implement broadcast receiver in your activity's to receive the broadcast. More info here: http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/

You can set the data that you want to send in the Intent by using putExtra.
But you can also store it in a database and by using the broadcasts you let the activity know that there is new data available

like image 99
user1051892 Avatar answered Sep 13 '26 08:09

user1051892