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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:06:10+00:00 2026-06-04T19:06:10+00:00

I want to pass a reference of the current activity to a method of

  • 0

I want to pass a reference of the current activity to a method of a class not extending Activity. I need to use this reference to the active activity so that I can use getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

I pass both the current context and activity to the class object using these setter methods in the class:

public class VersionCheck {

    public Context context;
    public Activity activity;

    //---------------------------------------------------------------------------------------------------------
    //set the current context
    //---------------------------------------------------------------------------------------------------------
    public void setContext(Context context) {

        this.context = context;
    }

    //---------------------------------------------------------------------------------------------------------
    //set the current activity
    //---------------------------------------------------------------------------------------------------------
    public void setActivity(Activity activity) {

        this.activity = activity;
    }   

The reference to the activity is used in an AsyncTask in the class (extract below):

//---------------------------------------------------------------------------------------------------------
//asynchronous task to check if there is a newer version of the app available
//---------------------------------------------------------------------------------------------------------
private class UpdateCheckTask extends AsyncTask<String, Void, String> {

HttpClient httpclient;
HttpPost httppost;
HttpResponse response;
InputStream inputStream;
byte[] data;
StringBuffer buffer;

protected void onPreExecute ()
{
    VersionCheck vc = new VersionCheck();

    //do not lock screen or drop connection to server on login
        activity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

        //initiate progress dialogue to block user input during initial data retrieval
        ProcessingDialog = ProgressDialog.show(context, "Please Wait", "Checking for Updates", true,false);
}

    @Override
    protected String doInBackground(String... currentVersion) 
    {

How do I get a reference to the Activity so that I can pass it to the setActivity(Activity) method? I use myActivity.this as reference for the context. Is it the same for Activity?

  • 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-04T19:06:11+00:00Added an answer on June 4, 2026 at 7:06 pm

    yes it will be same for Activity……………..

    http://developer.android.com/reference/android/app/Activity.html

    See here Context is super class of Activity……

    java.lang.Object
    
       ↳    android.content.Context
    
           ↳    android.content.ContextWrapper
    
               ↳    android.view.ContextThemeWrapper
    
                   ↳    android.app.Activity
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want pass a class-instace to a method of an other and be sure
I want to pass a self-reference down to an instantiated class (the child should
I want to pass a variable to he buttonEvent method in the selector. [button
I want to pass through configuration arguments to a class. These are all the
I want to pass around some values in an array that will always be
I want to pass external XML a variable. How do I do this? WHAT
I want to pass my UIImageView that has a image loaded into it from
How can I pass in the current datetime parameter to a SOAP request? I
Normally, when I want to call a dynamic method in another ILGenerator object that
I want to pass a reference of the DOM object to a jQuery function.

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.