Problem: Activity A is a ListView that contains a ListAdapter, and clicking in any item of the adapter leads to activity B. Activity B has a button that fetches a new item (or several) from the web (using an AsyncTask) and adds it to the list displayed by activity A when pressed. That operation from B is not blocked by a ProgressDialog, so the user can move back to A before the AsyncTask that B started finishes fetching the data.
So I need a way of updating the adapter of A from B.
I have a class C with static data displayed in the ListView by A. When the button at B is pressed, it adds that value to C. That class also has the adapter from A as a static field, but I think that this leaks the memory from the Context, and that is bad. My first idea of fixing this was removing the static adapter from C and every time A onResume() (and if the data on the adapter is different from what I have at C), I load the data from C again into the adapter and notifyDatasetChanged(). Well, it works most of the time, but if the user goes back to A from B before B fetches the data from the web, then the adapter does not update, since the onResume() came before the data is fetched.
Question: Is there a better way of updating the adapter of A from B?
Don’t save static references to the adapter. It will indeed leak memory and behave badly.
It appears I misunderstood the first time. Here is an updated answer:
First solution
The prettiest solution is to implement a content provider for the data storage and query that content provider in both A and B. Update the data in B using contentProvider.insert()
and read the data using contentProvider.query() returning for example a SQLiteCursor if it is backed by database or a MatrixCursor if you just save it in memory in the content provider.
The basic steps (without CursorLoader):
ContentResolver.registerContentObserver(uri, true, this)where uri is an URI using some scheme you set.ContentResolver.query(uri, projection, selection, selectionArgs, sortOrder)where projection, selection, selectionArgs and sortOrder can be what fits your contentprovider (maybe null). Uri refers to the data you want to query and is your choice.ContentResolver.insert(). Ininsertof your contentprovider you callContentResolver.notifyChange (Uri uri, null, null)where uri is the URI you used in step 1.Note that you will not need to call registerContentObserver at all if you use CursorLoaders!
It will receive the new cursor in the loader since it has automatic requery when you notify change.
Second solution
A less pretty solution is to implement a singleton object that handles the data.
Something like:
Implement the class
onStart()of DataListener to make sure that it is set if the change was done when B was alive usingDataHolder.getInstance().getData().DataHolder.getInstance().registerListener(this);Let the listener update the data.DataHolder.getInstance().unregisterListener(this)DataHolder.getInstance().setData(data)Also note that you can make the second solution fully thread safe by changing
void registerListener()tosynchronized String registerListenerAndGetValue()if you also make setValue synchronized.Old answer based on a misunderstanding
My old answer for general result handling did not quite answer the question, but it was:
If you want to send data back to an activity A you should do the following:
startActivityForResult (Intent intent, int requestCode)setResult (int resultCode)onActivityResult (int requestCode, int resultCode, Intent data)As an addition you could add so you only fetch data in A the first time by doing it only if
savedInstanceState == nullin for example onCreate().