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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:41:51+00:00 2026-06-15T22:41:51+00:00

I’m cursious about the soft and weak references in the Java environment. I have

  • 0

I’m cursious about the soft and weak references in the Java environment. I have also read a bit about both topics. Just to sum up, the weak reference are as the name says, weak references to an object. This means that the reference to this object is easily collected by the GC. A weak reference is created this way:

WeakReference<SomeOtherRef> weakReference = new WeakReference(someOtherRef); 

On the other hand a soft reference will stick around much longer than a weak reference. So my question is:

In my application, I have a custom adapter for a ListView This class will handle all the basic ListView stuff as handling clicks etc. When the user click on off the items in my list, a AsyncTask will be started.

convertView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                LoadCase loadCase = new LoadCase(position, holder);
                loadCase.execute("");

            }
        });

For now I dont show any progressDialog, for one reason. The context object. My problem is that the Activity which will initialize the CustomAdapter holds many object, and I have to pass a reference to the Activity Context in order to show a ProgressDialog, this will cause memory leaks, yes, I have tried. Is it safe to apply the Weak/Soft reference to deal with this? A WeakReference can be, at some time, null..this will cause a NullPointerException when I try to initialize my ProgressDialog.

  • 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-15T22:41:52+00:00Added an answer on June 15, 2026 at 10:41 pm

    Yes, it’s a good idea to use a WeakReference here, because the activity could be destroyed. You have to check if it’s null each time that you want to use it to avoid a NullPointerException. If it’s null, it means that the activity has been destroyed, and thus it usually doesn’t make sense to show a ProgressDialog.

    Of course, the activity could be destroyed and recreated (for example because of an orientation change). If the AsyncTask takes long, this could have the problem that the progress bar is no longer shown. But the problem here would be using an AsyncTask for a long task, they are intended for short tasks.

    Also, remember that if you subclass AsyncTask as an inner class in the Activity class, it will have a strong reference to the Activity, so using a WeakReference would be useless.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

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.