androidlistviewandroid-event

How to notify from B activity to A that list data has been changed?


I have a Activity ItemList.java. When user click on item, it starts new activity, let's say ItemDetailActivity.java.

What I want to do is, when the user presses a button on ItemDetailActivity.java, it should tell ItemList.java to refresh list item. I know that myListAdapter.notifyDataSetChanged() will refresh the list. But how do ItemList.java will know to when to change data.

I don't want to use a static method or variables. I know how to use handlers, but don't know how to pass handler to other activities.

What I am thinking is to create my own Event / Listener and associate with the list, so when event fires, then change list. Would a Broadcast receiver help me?


Solution

  • The best way would be to use the observer pattern (refer to http://en.wikipedia.org/wiki/Observer_pattern), but you could make a public static method in ItemList.java containing a call to myListAdapter.notifyDataSetChanged().