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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:08:43+00:00 2026-06-12T06:08:43+00:00

I usually delegate all Activities events to a separate controller class, which has a

  • 0

I usually delegate all Activities events to a separate controller class, which has a special method for handling events from Activity

@Override
public boolean handleMessage(int what, Object data) {
    switch (what) {
    case ExerciseViewEvent.STARTUP:
        workerHandler.post(new Runnable() {
            public void run() {
                onStartup();
            }
        });
        return true;
}

This is done in order to keep UI thread responsive and make all computations in background tasks.

However, when Activity.onDestroy() method is called by the system, controller.dispose() method is called, which cleans up all stuff in controller this way

@Override
protected synchronized void dispose() {
    .................
    if (model != null) {
        synchronized (model) {
            model.dispose();
        }
        model = null;
    }
    helper = null;
    .....................
    super.dispose();
}

As you can see, disposing of controller happens in UI thread, without delegating it to a handler thread.

The problem happens when, for example, onDestroy is called in the middle of onStartup() method: onDestroy cleans up the model and all other references, but inside onStartup method it tries to access the model at some point, but considering it is null, an exception is thrown.

What is the best way for resolving this issue? I do not want to lock every controller method because some of them may happen simultaneously without interfering each other.

  • 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-12T06:08:44+00:00Added an answer on June 12, 2026 at 6:08 am

    In the dispose() method you should cleanup the workerHandler before disposing of the model. See the removeCallbacks or removeCallbacksAndMessage(null) methods in the Handler class. The latter method removes all callbacks and messages when the argument is null.

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

Sidebar

Related Questions

With a classic method implementation, I usually perform BeginInvoke like this: private delegate void
Usually, in order to launch a new Activity and get its result, from an
The SKProductsRequestDelegate has one single method: - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response Usually, I find
I have a c++/cli wrapper class which grabs frames from a camera and sends
Usually, we only put the data we want to send as websocket.send() method's parameter,
I usually read a word 'delegate' in the apple document and their library book.
I usually use Dispatcher.Invoke when i need to access some UI elements from background
I have a view-based NSTableView that usually has one column in it. However, at
I need some help fixing a cross-thread exception. I am using Invoke which usually
I am trying to see a reason to use a delegate, and from what

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.