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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:18:54+00:00 2026-06-17T11:18:54+00:00

I am facing a very strange issue. I have an Activity , with the

  • 0

I am facing a very strange issue. I have an Activity, with the ActionBar containing a custom view.

If I load the XML of this custom view like this:

LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.action_search_form,null);

It leaks: the activity is not GC’d. The following fix works (why?):

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());

However if I set a OnClickListener on a child view it leaks again:

ImageButton clear = (ImageButton) vg.findViewById(...);
clear.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
            // Even if empty
        }
    });
}

All this happen in a private method of the activity and all views are local variables.

I found a fix but do not understand why it works: The view was simple so I coded it in Java instead of inflating the XML. I suspected it is related to the fact that no context is passed to the LayoutInflater and nothing happens out of the Activity itself, but if someone could help me understand what happened I would greatly appreciate it.

  • 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-17T11:18:55+00:00Added an answer on June 17, 2026 at 11:18 am

    All View classes require a Context parameter. When you are inflating the XML, the context provided to the created View is the same context provided to the LayoutInflater. Your view is retaining a reference to your activity.

    The reason why when you use getApplicationContext() and the Activity doesn’t leak is because you are passing a context that is global to the entire lifecycle of the application, not just the activity.

    ** EDIT **

    In regards to why the OnClickListener is also retaining your Activity, it is because of a side effect of using anonymous inner classes. By default, these inline implementations retain a reference to the parent / wrapping class instance. This is so you can call methods of the parent from the implementation of the inner.

    For example:

    myView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            MyActivity.this.doSomethingCool();
        }
    }
    

    Whether or not you have anything in the method body, the inner class still maintains a reference to the parent class.

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

Sidebar

Related Questions

I am facing a very strange issue. I have a SharePoint webpart that displays
This is a very strange issue I am facing for a while now, when
I am facing this very weird problem, I have created a custom UITableViewCell on
I am facing very strange issue. When I was running my app in iPhone
I've websphere application server 7 network deployment, and am facing a very strange issue,
I'm facing very strange issue, I'm getting JSON object from django powered site and
I am facing a very strange problem. I have a grid and when i
I am facing a very strange issue with dialog... I am writing an android
I am facing a very strange issue ever since I updated the Xcode with
Im facing with a very strange issue and i dont know yet the source

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.