4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105

3 Answers3

3

You could try startActivityForResult(intent_to_activity) and in the same itemlist class, override the method :onActivityResult(). In this method do the notify data set change and it should reflect.

Urban
  • 2,201
  • 24
  • 52
3

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().

jcxavier
  • 2,232
  • 1
  • 15
  • 24
1

You really shouldn't assume that a reference to your old activity will be kept alive by Android. If this is all based on a database, then you really don't need to do anything for your ItemList to update. If its not, you can simply call myListAdapter.notifyDataSetChanged() in the onResume() of your ItemList activity.

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • That's not fear sir :( I don't want to do this every time. – Arslan Anwar Dec 22 '11 at 14:19
  • Well, if you are absolutely sure the only time you will ever modify that data is in the ItemDetailView, then you could do what Urban suggested or set a static boolean flag somewhere. Then you just check that flag to see if you need to update your ItemList. Finally, its not at all uncommon practice to update your list in onResume. Its entirely possible that your activity is paused for days, weeks, or even months. – Justin Breitfeller Dec 22 '11 at 14:29