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 8700595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:12:05+00:00 2026-06-13T02:12:05+00:00

I have a singleton class that registers a preference change listener like: PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()) .registerOnSharedPreferenceChangeListener(

  • 0

I have a singleton class that registers a preference change listener like:

PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext())
            .registerOnSharedPreferenceChangeListener(
                    preferencesChangeListener);

When certain preferences are changed my onSharedPreferenceChanged method will notify interested components about an event. One of these interested components updates data in a ListAdapter. I’m getting some exceptions indicating that this list adapter is being modified by a thread that is not the UI thread.

I’ve reviewed the code paths that change the adapter data and don’t see anything wrong. My only suspiscion is that onSharedPreferenceChanged in the preference change listener is not being called on the UI thread.

The docs indicate that this method is called on the UI thread

This callback will be run on your main thread.

Has anyone seen situations where this is not the case? Does it matter that I registered my preference change listener with the application context?

Update: Here is the stack trace. The array size is always 0 with this exception. This is why I am taking a look at the shared preference listener since this is the only code path where I clear the data in the adapter. The index is non-zero, so it indicates there was data there before.

> java.lang.IndexOutOfBoundsException: Invalid index 51, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at com.palta.earthquake.EarthquakeAdapter.getItem(EarthquakeAdapter.java:56)
at com.palta.earthquake.EarthquakeAdapter.getView(EarthquakeAdapter.java:86)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.makeAndAddView(ListView.java:1727)
at android.widget.ListView.fillUp(ListView.java:682)
at android.widget.ListView.fillGap(ListView.java:628)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2065)
at android.widget.ListView.onTouchEvent(ListView.java:3315)
at android.view.View.dispatchTouchEvent(View.java:3765)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:905)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
at android.app.Activity.dispatchTouchEvent(Activity.java:2093)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
  • 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-06-13T02:12:08+00:00Added an answer on June 13, 2026 at 2:12 am

    Are you running the app on Android <=2.2? If yes, commit() implementation changed in 2.3 so that it’s always executed in main thread (hence onSharedPreferenceChanged() too), but not in 2.2.

    See this thread.

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

Sidebar

Related Questions

I have a class that is a singleton. This singleton is instantiated using GCD
I have an object of some class that obeys the singleton pattern. I need
I have a Singleton that is accessed in my class via a static property
I would like to create a singleton class that is instantiated once in each
I have a couple classes that look like this public class ParentClass { public
I have a singleton class that will initialize it's data from a web service
Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits
I have a class that is essentially a message manager (a singleton) that has
I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet
I have a C# singleton class that multiple classes use. Is access through Instance

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.