Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an intent to update an activity?

I have a service that is downloading a file. When the download is done, I would like to update my "Downloaded files" list in my Activity, but only if the Activity is running. I do not want the Activity to start if it's not already running.

I was hoping I could do this by making a new Intent with some special flag.

Anyone have any idea how I can achieve this? A tiny code example maybe?

like image 967
Vidar Vestnes Avatar asked Aug 24 '09 19:08

Vidar Vestnes


People also ask

How do I use intent to launch a new activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);

What is the intent of an activity?

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity() . The Intent describes the activity to start and carries any necessary data.

How do you call the second activity from the first activity which intent is used?

Intent in = new Intent(getApplicationContext(),SecondaryScreen. class); startActivity(in); This is an explicit intent to start secondscreen activity.


1 Answers

You can create new BroadcastReceiver instance and do something along these lines on your Activity's onResume() method:

registerReceiver(myReceiver, new IntentFilter(DownloadService.ACTION_FILE_DOWNLOADED));

After that, override myReceiver's onReceive() method to call a function that updates the component you want:

@Override 
public void onReceive(Context context, Intent intent) {
...
    updateViewWithData(service.getNewFileData());
...
}

On your Activity's onPause() method, just unregister the receiver:

unregisterReceiver(myReceiver);

I hope that this would help you, feel free to ask if there is something unclear.

like image 144
Dimitar Dimitrov Avatar answered Nov 04 '22 18:11

Dimitar Dimitrov