Shivendra Saxena
Shivendra Saxena

Reputation: 49

Update an activity by events of a background activity

I have 2 activities A and B. A has a recyclerView with list of songs. When i click one command goes to B and it starts playing it. B has an onCompletionListener() so it changes and plays song automatically. When one hovers back to activity A, name of currently playing song is displayed in a textView there. Problem is that when a song is is changed in background, the textView can't be automatically updated. One solution I had thought is making that textView static and change it from OnCompletionListener in B but I don't find that efficient. Is there a better way of doing that.

Upvotes: 3

Views: 58

Answers (2)

lucasjensen
lucasjensen

Reputation: 41

There are several ways to implement this:

  • Handler You can implement Handler in A and pass the address of Handler to B. And you can send message to A using this Handler.
  • BroadcastReceiver Same as Handler. You can implement in A and pass the address of BroadcastReceiver to B. And you can send broadcast to B using this.
  • startActivityForResult You can start activity B by using startActivityForResult. You need to implement onActivityResult function in A. You can send data to A when B is finished. You can send last song data to A when B is finishing.

Upvotes: 1

Giuseppe Giacoppo
Giuseppe Giacoppo

Reputation: 435

If you're newbie in Android, the easiest (and fastest) way to create a communication channel between Android components is using a bus like EventBus

You just have to create a class containing the informations, then your activities will subscribe to a bus and they will send and receive these objects each other very easily

Upvotes: 1

Related Questions