0

I found a lot of answers about communicating between fragment and it's Activity but it was always description of communication between fragment and it's own Activity. I couldn't find a description how to communicate between fragments in different Activities.

I have the following scenario: I have ItemsActivity containing ItemsFragment with a list of items. User scrolls through items and clicks to some item. Then ItemsFragment creates an instance of new Activity: DetailActivity containing DetailFragment where is implemented detail view of item. User modifies data of the item and clicks back (or to some save button). Data of the modified item is saved and the DetailActivity is destroyed and removed from back stack. User is back in the ItemsActivity/ItemsFragment containing a list of items.

Question: The DetailFragment should notify the ItemsFragment, that the item was modified so the ItemsFragment should update it's view in the item list. Problem is that the DetailFragment knows nothing about the ItemsFragment in the different Activity (ItemsActivity). How to notify the ItemsFragment in the ItemsActivity from the DetailFragment, that item was changed and the item's view in the list shoud be updated?

Fipil
  • 291
  • 2
  • 5
  • 16
  • 2
    you should probably be store your data somewhere that is easily accessible like a database that way you just pull again in your onResume of the activity – tyczj Oct 11 '13 at 20:53
  • You can Simply Register a BroadcastReceiver and call the receiver to ItemsFragment – Software Sainath Oct 11 '13 at 21:22
  • Thanks! I'll study the BrodcastReceiver usage. – Fipil Oct 12 '13 at 11:19
  • Is DetailActivity necessary? – Jumpa Oct 12 '13 at 12:37
  • The ItemsFragment has all the information it needs already doesn't it? It knows what item was clicked, and therefore what item might potentially be changing. So, when ItemsFragment resumes, it could just check for any updates to the item of interest in your data store. – Scott W Oct 12 '13 at 19:45
  • You're right Scott, but there is an another possible scenario: user creates a new item and it doesn't matter which part of app initiates this creation process. Then ItemsFragment should receive a notification. So the creating process sends broadcast intent to inform any receiver about the change. I thing that it's more elegant that your proposal. – Fipil Oct 12 '13 at 21:44

2 Answers2

1

Thanks to Software Sainath's comment I solved the communication between two fragments by using of the LocalBroadcastManager. It's described here. A very useful example of using of the LocalBroadcastManager is here.

Community
  • 1
  • 1
Fipil
  • 291
  • 2
  • 5
  • 16
-1

I stole this code from this answer: Sending data back to the Main Activity in android .

Start the DetailsActivity from ItemsActivity with startActivityForResult().

In DetailsActivity set the result to send back whatever information is appropriate. Since it appears you are saving all the information to disk, this can simply be some way of identifying what item needs to be read from disk and updated or you could instead pass back all of the information needed to update the item. Use resultIntent.putExtra() to attach the information you want to send back to ItemsActivity.

Do this when you have saved new data and want the item to go back to ItemsActivity and update the item:

Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(ItemsActivity.RESULT_CHANGED, resultIntent);
finish();

Override the onActivityResult() method in ItemsActivity. In the following, "requestCode" would be the integer put into startActivityForResult() to identify what was requested. "resultCode" is what you put into setResult().

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
    case (MY_CHILD_ACTIVITY) : {
      if (resultCode == ItemsActivity.RESULT_CHANGED) {
        // TODO Extract the data returned from the DetailsActivity and update item 
      }
      break;
    } 
  }
}

This code assumes you have a constant RESULT_CHANGED defined in ItemsActivity. "requestCode" is an int passed into startActivityForResult() that you use to identify where the result is coming from.

Community
  • 1
  • 1
Immanuel
  • 19
  • 2
  • Thank you for answer, but is this method really right? I thought that starting activity for result should be used in case, when the calling activity really expects any result, for example starting camera intent to make a picture and similar. But It's not too prescriptive in my scenario? – Fipil Oct 12 '13 at 11:15
  • startActivityForResult() is used when you want to get feedback from the activity you are starting. It may be something more complicated, but from what I can tell it is frequently used just to get back a string or a boolean value. If it is quick for you to read the data from disk and generate the list, the easiest way may be just to populate the list in onStart(). – Immanuel Oct 12 '13 at 16:47
  • Yes, but it's too binded to the described scenario. The result is returned only to the calling activity. But what if the DetailFragment is created in another activity as a result of different user's behavour. The ItemsActivity should be notified anyway even if it's not an initiator of the change. So I think that better solution is to use BroadcastReceiver. See my own answer to my own question. – Fipil Oct 12 '13 at 17:27