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

The Archive Base Latest Questions

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

Inside of the handler after I first enter the app from a clean state,

  • 0

Inside of the handler after I first enter the app from a clean state, the Handler handles the MSG_PULLED action however, the reference to main is null. The weak reference is not null. How can this possibly be happening?

Inspired by this post: This Handler class should be static or leaks might occur: IncomingHandler

static class MainHandler extends Handler {
    private final WeakReference<MainActivity> wMain; 
    static int angle=0;
    public MainHandler(MainActivity main) {
        super();
        this.wMain = new WeakReference<MainActivity>(main);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        MainActivity main = wMain.get();
        int what = msg.what;
        if(what == MSG_PULLED) {
            main.startAnim();
        } 
    }
}

And how I initiate the handler:

static MainHandler mainHandler;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainHandler = new MainHandler(this);
        mainHandler.sendEmptyMessageDelayed(MSG_PULLED,500);
    }
  • 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-12T03:13:24+00:00Added an answer on June 12, 2026 at 3:13 am

    The Activity instances in your app are regularly destroyed and new ones are created for example when rotating the display.

    Now what should happen in that case is that the old instance is garbage collected and only the new one exists. If you keep the old one around you have created a leak.

    Since Handler are not garbage collectable until they have no more messages (?) they can live longer than the Activity in witch they were created, which usually leads to leaking the old Activity (until the Handler can be collected) since Handler usually have a strong reference to their Activity.

    The WeakReference way in your code get’s rid of that problem by keeping just a weak link to the Activity that does not prevent garbage collection.

    The problem is that you use the get() method the wrong way: get() will only return the original object while it exists. When it’s gone you get null. Here: the Activity will exists while it is still the active one (determined by the system).

    The null is also not a big problem: when you get null your Activity instance is no longer alive (maybe a new one was created, maybe it’s completely gone) so you can’t do anything useful with it anymore. Animation would not show even if you had still a reference.

    Basically do it like below and your problem is solved

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        MainActivity main = wMain.get();
    
        // message arrived after activity death
        if (main == null) return;
    
        int what = msg.what;
        if(what == MSG_PULLED) {
            main.startAnim();
        } 
    }
    

    The WeakReference itself (wMain) is not null because it is itself strongly references as a member variable. Just the content inside it can / will be null at some point.

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

Sidebar

Related Questions

Why is calling a standard library function inside a signal handler discouraged?
Does anybody know the trick, how to free control inside its event handler ?
I am creating linkbuttons inside of a panel and hooking up an event handler
I've seen this done inside of an event handler directly behind the .xaml file
i have client-server app, I'm managing connections with Threads and handlers inside app, but
I have a wxpython app designed using XRC which has a multiline textctrl inside
I have this code: package org.example.Threading; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message;
I'm running a handler inside of a while loop, against a lat/lng span so
I have added an event handler for itemOpening event for the tree component inside
I have 3 python modules. LogManager.py Runner.py Other.py Runner.py is the first main module

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.