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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:03:22+00:00 2026-06-10T23:03:22+00:00

I have a class that extends AsyncTask to send an ethernet message. When created

  • 0

I have a class that extends AsyncTask to send an ethernet message. When created and called within my MainActivity it works fine:

new SendCommandTask(MainActivity.this).execute(ipAddress, portNum, message);

However, I would like to embed this within a method in a new class instead…

public class NewClass{

   String ipAddress;
   String portNum;
   String command;

   public NewClass(String ip, String pn, String, c){
      ipAddress = ip;
      portNum = pn;
      command = c;
   }
   public send(){
      SendCommandTask(MainActivity.this).execute(ipAddress, portNum, command);
   }
}

… and call the method directly from my MainActivity:

NewClass newClass = new NewClass;    
NewClass.send();

I have tried passing the activity as another parameter in the constructor, but the method pukes when I do that. Is there another way to refer to the calling Activity in my NewClass? Or am I stuck running the AsyncTask from the MainActivity?
(I hope this makes sense, I’m very new to Java and OO programming in general.) Any help is very much appreciated!

  • 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-10T23:03:24+00:00Added an answer on June 10, 2026 at 11:03 pm

    Please see this answer that I wrote a couple of days ago. I think it applies here as well. The main additional consideration is, does your AsyncTask need to be able to outlive its calling Activity or not? If there’s any chance that the Activity will be closed or restarted, either kill the task in onStop() or be careful not to use any shared fields.

    If your AsyncTask is going to directly read any members that are part of the Activity that you are passing in, you may as well just keep it inside the Activity. Factoring out the task into a freestanding class is best done when you want it to be independent and able to work with several Activity classes.

    Also, consider doing your work in a Service instead of an Activity. I’m a big fan of IntentService.

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

Sidebar

Related Questions

I have created a new class called Get_WebPage1 and init I have extends that
I have a async task called within an activity: public class DownloadFile extends AsyncTask<String,
I have one class called trailsActivity that extends Activity. I create a new instance
I have a class that extends AsyncTask , which fetches the gps cordinates from
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have class that extends AsyncTask class and I create instance of this class
I have an Activity Class with an inner protected class that extends AsyncTask. I
I have a nested inner class that extends AsyncTask to run a db query
So I have this loader class which extends AsyncTask. Then I do new loader().execute();
I have a public class FriendMaps extends MapActivity that gets called from a menu

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.