Skip to content Skip to sidebar Skip to footer

Android Update Current Activity From Background Thread

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 sta

Solution 1:

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

publicclassMyAsyncTaskextendsAsyncTask<Void, Void, Void> {

    private Callback mCallback;

    private boolean mIsComplete = false;

    private boolean mHasCallbackBeenCalled = false;

    publicMyBackgroundTask(Callback callback) {
        mCallback = callback;
    }

    /** Only safe to call this from the UI thread */publicvoidattach(Callback callback) {
        mCallback = callback;

        if (mIsComplete && !mHasCallbackBeenCalled) {
            fireCallback();
        }
    }

    /** Only safe to call this from the UI thread */publicvoiddetach() {
        mCallback = callback;
    }

    @Override
    publicvoiddoInBackground() {
        // do the heavy stuff herereturnnull;
    }

    @Override
    publicvoidonPostExecute(Void result) {
        mIsComplete = true;
        fireCallback();
    }

    privatevoidfireCallback() {
        if (mCallback != null) {
            mCallback.callbackMethod();
            mHasCallbackBeenCalled = true;
        }
    }

    publicstaticinterfaceCallback {

        publicvoidcallbackMethod();

    }
}

MyActivity

publicclassMyActivityextendsActivityimplementsMyAsyncTask.Callback {

    privateMyAsyncTask mTask;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Check for a retained task after a configuration change// e.g. a rotationif (getLastNonConfigurationInstance() != null) {
            mTask = (MyAsyncTask) getLastNonConfigurationInstance();
            // Re-attach the task
            mTask.attach(this);
        }
    }

    @OverridepublicvoidonDestroy() {
        // Detach from task to avoid memory leakif (mTask != null) {
            mTask.detach();
        }

        super.onDestroy();
    }

    @OverridepublicObjectonRetainNonConfigurationInstance() {
        // Retain the async task duration a rotationreturn mTask;
    }

    /** Callback method */@OverridepublicvoidcallbackMethod() {
        // Do something here
    }

}

Solution 2:

You could make a singleton in which you will have your thread and a queue of "tasks". When a task is finished, you check / launch the next task, and when you add a task, you launch it, or add it in the queue if a task is already running.

I don't say this is the best solution, but it's one.

Post a Comment for "Android Update Current Activity From Background Thread"