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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:09:34+00:00 2026-06-10T15:09:34+00:00

It took my quite a while to get this to work, but It’s clearly

  • 0

It took my quite a while to get this to work, but It’s clearly not best practice. In short, I need to show a dialog when my AsyncTask finishes, but getApplicationContext() does not work, neither does passing it as a parameter when creating the AsyncTask. So I’ve declared a public variable for the context in my AsyncTask class and set it before I execute:

    private OnClickListener clickLoadRefs = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("H","Clicked Load Refs");
        RefreshRefPoints refreshRefPoints = new RefreshRefPoints();
        refreshRefPoints.myCtx=v.getContext();
        refreshRefPoints.execute(v.getContext());
    }
};

private class RefreshRefPoints extends AsyncTask<Context, Integer, Integer> {

    private Integer nPoints=0;
    public Context myCtx;
    private ProgressDialog pd;

    protected Integer doInBackground(Context... ctx) {
        Log.d("H","doInBackground()");
        dbHelper.clearRefPoints();
        requestRefPoints();
        nPoints = parseRefPointsCSV();

        return nPoints;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPreExecute() 
    {
        pd = ProgressDialog.show(myCtx, "Refreshing Reference Points", "Loading...", true,false);
        Log.d( "H", "onPreExecute()" );
    }
    protected void onPostExecute(Integer result) {
        pd.dismiss();
        AlertDialog.Builder builder = new AlertDialog.Builder(myCtx);
        builder.setTitle("Reference points refresh complete");
        builder.setMessage(result+" records loaded");
        builder.setPositiveButton("OK",null);
        builder.show();
        Log.d("H","onPostExecute()");       
    }...etc

Can anybody just show me the proper way of passing the context?

Thanks

  • 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-10T15:09:36+00:00Added an answer on June 10, 2026 at 3:09 pm

    Define a constructor method and pass context a parameter. It would be better.

    Here what I meant:

    private class RefreshRefPoints extends AsyncTask<Void, Integer, Integer> {
    
        private Integer nPoints=0;
        private Context myCtx;
        private ProgressDialog pd;
    
        public RefreshRefPoints(Context ctx){
            // Now set context
            this.myCtx = ctx;
        }
    
    }
    

    That’s all.

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

Sidebar

Related Questions

Preface: This has become a quite a long post. While I'm not new to
OK, this is probably pretty basic stuff, but it took me quite some time
Been trying to do this for quite a while and took advice from every
We've been debugging this JBoss server problem for quite a while. After about 10
I've been having trouble searching for this answer because I am not quite sure
This is quite an interesting question so let me set the scene. I work
It's been quite a while since I took data structures and algorithms in college,
This seems to be like a common use case... but somehow I cannot get
Alright, first off I'm not quite sure how to phrase my problem. This could
it took me some time to find out that both Eclipse and Aptana get

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.