Make the application interact with the current activity

My Android application uses a class Application

(namely its subclass Application

) to start a thread to fetch data from a website. Once it does it successfully, it should provide this data to the current one Activity

(there are 2 actions in my application). How can I report this Activity

? How can I tell the current Activity

one that there is new data available that needs to be retrieved and submitted? Thank you for your responses.

+3


source to share


3 answers


How about using a broadcast system.

You can get the context in the Application class with 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 a broadcast receiver in your activity to receive the broadcast. More details here: http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/

You can set the data you want to send to the Intent using putExtra.
But you can also save it to the database and using the broadcasts you give the activity that there is new data available.

+2


source


If you save this data to the content provider (which you should if the data is not that small), you can register a content watcher with the content provider. Action must be managed. Look here



0


source


This is very bad practice as you never know when the application object will be destroyed. I recommend using the service instead. Read it on Android Developers!

0


source







All Articles