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

  • Home
  • SEARCH
  • 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 8276663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:26:13+00:00 2026-06-08T08:26:13+00:00

My application has a refresh button on the main activity. When the user presses

  • 0

My application has a refresh button on the main activity. When the user presses that button, a new thread is created which starts updating the SQLite database. When this thread started, user could possibly get into another activies of the application.

The problem is these other activities(ListActivity) should be updated according to the DB when that background thread is completed. How could I provide that. I tried getting current task with ActivityManager but It requires extra permission which I dont want.

  • 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-08T08:26:15+00:00Added an answer on June 8, 2026 at 8:26 am

    Edit:

    Sorry seems I misunderstood you. Please take a look at the following code, it is similar to Chinaski’s (you just use an interface for the callback methods) but I added a bit more to ensure you know how to use it in a way that will avoid memory leaks.

    Note how the activity detaches during onDestroy — alternatively you could use a WeakReference, however these days you’d use a Fragment with setRetainInstance(true) and completely avoid the detaching/attaching as the fragment would be retained.

    MyAsyncTask

    public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    
        private Callback mCallback;
    
        private boolean mIsComplete = false;
    
        private boolean mHasCallbackBeenCalled = false;
    
        public MyBackgroundTask(Callback callback) {
            mCallback = callback;
        }
    
        /** Only safe to call this from the UI thread */
        public void attach(Callback callback) {
            mCallback = callback;
    
            if (mIsComplete && !mHasCallbackBeenCalled) {
                fireCallback();
            }
        }
    
        /** Only safe to call this from the UI thread */
        public void detach() {
            mCallback = callback;
        }
    
        @Override
        public void doInBackground() {
            // do the heavy stuff here
            return null;
        }
    
        @Override
        public void onPostExecute(Void result) {
            mIsComplete = true;
            fireCallback();
        }
    
        private void fireCallback() {
            if (mCallback != null) {
                mCallback.callbackMethod();
                mHasCallbackBeenCalled = true;
            }
        }
    
        public static interface Callback {
    
            public void callbackMethod();
    
        }
    }
    

    MyActivity

    public class MyActivity extends Activity implements MyAsyncTask.Callback {
    
        private MyAsyncTask mTask;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Check for a retained task after a configuration change
            // e.g. a rotation
            if (getLastNonConfigurationInstance() != null) {
                mTask = (MyAsyncTask) getLastNonConfigurationInstance();
                // Re-attach the task
                mTask.attach(this);
            }
        }
    
        @Override
        public void onDestroy() {
            // Detach from task to avoid memory leak
            if (mTask != null) {
                mTask.detach();
            }
    
            super.onDestroy();
        }
    
        @Override
        public Object onRetainNonConfigurationInstance() {
            // Retain the async task duration a rotation
            return mTask;
        }
    
        /** Callback method */
        @Override
        public void callbackMethod() {
            // Do something here
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has one activity which starts two services but does not bind them.
Our application has many controls that are created dynamically. For example, a navigation pane
I have a widget that has a refresh button and a textview. Refresh updates
I have android application with ListView, where each row has own refresh button. Refreshing
2nd Tab when input new values doesnt refresh. The application has to be restarted
I have built an completely AJAX based application which has no page refresh and
I have a Silverlight application which has on it a Canvas. On that Canvas,
Hi I have an application which after login, loads a main activity which contains
My application has multiple SQLite tables(around 20 with different fields). This I have created
My application has two layouts A and B. In A a user chooses some

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.