Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:29:03+00:00 2026-05-29T22:29:03+00:00

Problem: Activity A is a ListView that contains a ListAdapter , and clicking in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:29:03+00:00Added an answer on May 29, 2026 at 10:29 pm

    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):

    1. In onCreate of A you register yourself as a contentobserver using ContentResolver.registerContentObserver(uri, true, this) where uri is an URI using some scheme you set.
    2. In onCreate of A you get the data by querying the contentprovider using 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.
    3. When data is loaded in B you call ContentResolver.insert(). In insert of your contentprovider you call ContentResolver.notifyChange (Uri uri, null, null) where uri is the URI you used in step 1.
    4. Implement onChange(boolean selfChange) in A and requery the content provider when it is called.

    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:

    1. Implement the class

      public class DataHolder {
          private static DataHolder sDataHolder;
      
          private String data = ""; // Data represented by string.
          public DataHolder getInstance() {
              if (sDataHolder == null) {
                  sDataHolder = new DataHolder()
              }
              return sDataHolder;
          }
      
          private DataHolder() {} // Hidden constructor
      
          public void setData(final String data) {
              mData = data;
      
              for (DataListener listener: mDataListeners) {
                  listener.onDataChanged(mData);
              }
          }
      
          public void registerListener(DataListener listener) {
             mDataListeners.add(listener);
          }
      
          public String unregisterListener(DataListener listener) {
             mDataListeners.remove(listener);
          }
      
          public String getData() {
             return mData;
          }
      
          public static interface DataListener {
              public void onDataChanged(String data);
          }
      }
      
    2. Make A implement DataListener
    3. Read and update the data in onStart() of DataListener to make sure that it is set if the change was done when B was alive using DataHolder.getInstance().getData().
    4. Register listener in A’s onCreate/onStart using DataHolder.getInstance().registerListener(this); Let the listener update the data.
    5. Unregister listener in A’s onDestroy/onStop using DataHolder.getInstance().unregisterListener(this)
    6. Set the data and signal any listener in B using DataHolder.getInstance().setData(data)

    Also note that you can make the second solution fully thread safe by changing void registerListener() to synchronized 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:

    1. Always start B with startActivityForResult (Intent intent, int requestCode)
    2. Set the result when done in B using setResult (int resultCode)
    3. Handle the result when you come back to A by implementing 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 == null in for example onCreate().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my problem : I have an Activity that includes a ListView with different
I have a listView using a custom adapter. Each row contains a button and
Problem: during execution, instances of classes that derives from System.Workflow.ComponentModel.Activity is serialized by the
This seems like a simple problem: I have a WF4 activity that guides the
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
I want to test the functionality of an activity that contains a ListFragment ,
I have a listview that contains values from webservice.Each page contains only 10 listitems
I have a Custom ListView that contains a vertical LinearLayout of horizontal LinearLayouts for
My android-app gets List in the one activity, but activity,that contains the LisView for
I have an android-app with a listview in an activity. The listview has, if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.