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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:46:01+00:00 2026-06-17T18:46:01+00:00

Well, I have an activity class with two background task (Async-Task) which have been

  • 0

Well, I have an activity class with two background task (Async-Task) which have been defined in two separate classes like

public class GettingBeaconsList extends AsyncTask<String, String, String> 
public class GettingAirports extends AsyncTask<String, String, String> 

which are initialized and executed in MainClass

public class MainClass extends Activity implements DelegateTaskCompleted
{

     int ServiceBoolean = 0;
     public OnClickListener LoadingBeaconList = new OnClickListener()
     {
         public void onClick(View v)
         {
             ServiceBoolean  =1;
             new GettingBeaconsList (context,MainClass.this).execute();
         }
     }

    public OnClickListener LoadingAirportList= new OnClickListener()
    {
         public void onClick(View v)
         {
             ServiceBoolean  =2;
             new GettingAirports(context,MainClass.this).execute();
         }
    }


    @Override
    public void JsonArrayLoaded(JSONArray result) 
    {
        // bla bla or whatever here i write, does not matter
         if(ServiceBoolean  == 1)   { 
                //  Load activity 5
         }

         else if( ServiceBoolean  == 2)
         { 
             //  Load activity 6

         }

        else if( ServiceBoolean==3 )
        {
            // display Toast or Alert Box or load Activity number 8
        } 


    }

}

Now in above code MainClass.this is stored as Interface Reference in AsynTask SubClasses like this

private Context context             =   null;
private DelegateTaskCompleted delegate      =   null;

public GettingBeaconsList (Context context,DelegateTaskCompleted delegate)  
{   
    this.context        =   context;
    this.delegate       =   delegate;
}

// And constructor of second AsynTask is same as of First AsynTask Class

private Context context             =   null;
private DelegateTaskCompleted delegate      =   null;

public GettingAirports (Context context,DelegateTaskCompleted delegate) 
{   
    this.context        =   context;
    this.delegate       =   delegate;
}

onPostExecute of each AsynTask class or subclass, JSONArray is returned or passed back to the calling class, shown below. In this case calling class is MainClass but there are other activity classes which use same AsynTask Classes(GettingBeaconsList and GettingAirports)

protected void onPostExecute(String file_url)   
{           
    pDialog.dismiss();      
    delegate.JsonArrayLoaded(gotNearestbeacons);
}

Now I have one method (JsonArrayLoaded) in MainClass to tackle two response coming from two different background task or services. I am using condition to figure out which service/class or AsynTask is executed.

But I am asking for the best way to tackle such scenario as if we have 5 or more background services in future and they just also return a JSON Array so do I need to make separate interfaces for each services ?

What should be object oriented way out to this case ?

  • 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-17T18:46:03+00:00Added an answer on June 17, 2026 at 6:46 pm

    The most simplistic solution I can think of is to modify your DelegateTaskCompleted interface so as it looks like this:

    public interface DelegateTaskCompleted{
      public void JsonArrayLoaded(AsyncTask<String, String, String> task, 
                                  JSONArray result);
    }
    

    Then your onPostExecute will send itself back like below:

    protected void onPostExecute(String file_url)   
    {           
        pDialog.dismiss();      
        delegate.JsonArrayLoaded(this, gotNearestbeacons);
    }
    

    Finally, in your MainClass, you can have a conditional check based on the type of AsyncTask:

     @Override
     public void JsonArrayLoaded(AsyncTask<String, String, String> task,
                                 JSONArray result) 
        {
             if(task instanceof GettingBeaconsList) { 
                    //  do stuff related to the beacon list
             }
    
             else if(task instanceof GettingAirports) { 
                // do airport list stuff
             }
    
        }
    

    That way you can easily identify the AsyncTask that sends through the response without having to track which it is, if one takes longer than the other etc.


    Alternatively, have another class that extends AsyncTask but adds an abstract variable for identification.

    public class TrackableAsyncTask extends AsyncTask<String, String, String>{
        public abstract int getId();
    
        public static final int ID_AIRPORT = 1;
        public static final int ID_BEACONS = 2;
        ... etc
    }
    

    Then have your Airport and Beacon AsycTasks extend this instead. This will require them to implement the getId method.

    public class GettingAirports extends AsyncTask<String, String, String> {
         public int getId(){
            return ID_AIRPORT;
         }
    }
    

    And then instead of doing a conditional if (task instanceof GettingAirports) you can do a switch statement like below:

    switch(task.getId()){
       case TrackableAsyncTask.ID_AIRPORT:
           // run airport code
    }
    

    Hope this helps.

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

Sidebar

Related Questions

I have two classes in question. Both extend Activity. Class A public void displayinfo()
I have an android activity in which I'm using tabs. public class UnitActivity extends
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called
I have an activity that shows some TextView s and EditText s, as well
well i have a configuration like this in the components part of my config
Well I have been writing in the same style for awhile now and all
Well i have a gridview where i have defined the columns on my own
Well i have a transparent div or the background is set to transparent :)
I have two activities, one is main and I have another activity called Test
I have two activities, say Activity A and Activity B . Activity A is

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.