Rynardt
Rynardt

Reputation: 5557

android.content.Context vs android.app.Activity

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?

Upvotes: 3

Views: 4734

Answers (1)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

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

Upvotes: 4

Related Questions