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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:07:27+00:00 2026-06-12T05:07:27+00:00

my AsyncTask doInBackground have multiple nested condition which return String to onPostExectue method ,

  • 0

my AsyncTask doInBackground have multiple nested condition which return String to onPostExectue method , which make code very complex is there any alternative way to handle this problem

Now my code look like this

protected String doInBackground(Void... params){
    if (position == 0){ 
         return "Set1" 
    }
    else if(Position >something) { 
         return set 2 
    } .........and so on 
}

@Override
protected void onPostExecute(String result) {
    if (result.equals("set1")){
       // do some taskk
    }
    elseif(result.equal("Set2")){
        // do other task
    }
    else if(){
       // sooo onnnnnn
     }
} 

thanks in advance for your help

  • 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-12T05:07:28+00:00Added an answer on June 12, 2026 at 5:07 am

    One can try to apply Strategy pattern here. You can create an abstract class, say Action, with an abstract function perform(). Then you can create concrete subclasses of Action for every action which can be performed in onPostExecute. So you have classes say ActionForSet1, ActinForSet2 etc with concrete implementations of perform(). Then Action should have a static method String Action createfromString(String set) which will be essentially the same if/else or break but much more maintainable. It will return corresponding Action subclasses for every string. You can use Map instead of if/else in there, easier to add/remove elements then. Then after you have your Action object returned by Action.createFromString you call its perform()

    onPostExecute(String result) {
       Action a = Action.actionForResult(result);
       a.perform();
    }
    
    abstract class Action {
      abstract void perform();
    
      static Action actionForResult(String result) {
          if(result.equals("res1") {
              return new ActionForRes1();
          } else if(result.equals("res2") {
              return new ActionForRes2();
          } else throw new IllegalArgumentException("No action for result " + result);
       }
    }
    
    class ActionForResult1 extends Action {
      @Override
      void perform() { Log.i("ACTION", "Here's action for result 1"); }
    }
    
    class ActionForResult2 extends Action {
      @Override
      void perform() { Log.i("ACTION", "Here's action for result 2"); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a AsyncTask in which an activity is launched during the doInBackground method:
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have an AsyncTask which calls my LocationHandler class method getLocation() which runs a
I have this code fragment: public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
I have used to following idiom on the doInBackground method of an AsyncTask for
In my application I have two threads (I'm using AsyncTask class). in doInBackground() method
I have an AsyncTask which launches a DatabaseHelper class from DoinBackground which copies an
I have this code so far: private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
I have this code: private class DownloadWebPageTask extends AsyncTask<String, Void, String> { @Override protected
I have a very strange problem. I have an AsyncTask which appears to block

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.