1

I have a class "HomeActivity", which is as follows:

        public class HomeActivity extends FragmentActivity implements OnClickListener {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                FragmentManager fm = getSupportFragmentManager();

                // Create the list fragment and add it as our sole content.
                if (fm.findFragmentById(android.R.id.content) == null) {
                    HomeFragment list = new HomeFragment();
                    fm.beginTransaction().add(android.R.id.content, list).commit();

                }
            }
        public static class HomeFragment extends Fragment {

        webServiceTask = WebServiceTask.getInstance(
                                    getActivity(), Constants.METHOD_NAME_PRODUCTS,
                                    Constants.PRODUCT_NAME, null);

public  void Work() {}
    }
    }

I have another class WebServiceTask, which is as follows:

  final public class WebServiceTask extends AsyncTask<String, String, String> {
    private WebServiceTask(final Activity activity, final String methodName,
                final String productName, final String addInfo[]) {
            super();
            this.activity = activity;
            this.methodName = methodName;
            this.productName = productName;
            this.addInfo = addInfo;
        }
public static WebServiceTask getInstance(final Activity activity,
            final String methodName, final String productName,
            final String additionalInfo[]) {
        webServiceTask = new WebServiceTask(activity, methodName, productName,
                additionalInfo);
        return webServiceTask;
    }
    protected void onPostExecute() {

    // Here I am trying to call the work() method in HomeFragment, How can I do that?
    }

My question is how can i call the work() method in HomeFragment class from onPostExecute().

2
  • work method is in HomeFragment class. Also i have activity variable. Commented Dec 13, 2013 at 10:45
  • ohh i didn't see that. Commented Dec 13, 2013 at 10:46

4 Answers 4

1

I would propose making a listener for you task, and invoke its method in post execute. It will geve you a lot more flexibility and control on what you want to deafter the task finishes. Here is sample code I would use:

public class MyTask extend AsyncTask<Void, Void, Boolean> {

    public interface MyTaskListener {
         void onSuccess();
         void onFailure();
         void onError(Throwable t);
    }

    private Throwable error;
    private MyTaskListener listener;


    public MyTask(MyTaskListener listener) {
          this.listener = listener;
    }

    @Overrride
    public Boolean doInBackground(Void... params) {

        try {
            if (workCompleted()) { 
                //work completed without error - return true
                return Boolean.TRUE;
            } else {
                //work failed to complete - return false
                return Boolean.FALSE;
            }
        } catch(Exception e) {
            //unexpected error  happened - remember error and return null
            this.error = e;
            return null;
        }  


    }


    @Override
    public void onPostExecute(Boolean result){
         if (!isCancelled()) { //you only want to process if task wasn't cancelled

             if (this.error != null && result == null) { //we have error, process it
                 if (listener != null) {
                     listener.onError(this.error);
                 }
             }

             if (Boolean.FALSE.equals(result)) { //we have faile, process it
                 if (listener != null) {
                     listener.onFail();
                 }
             }

             if (Boolean.TRUE.equals(result)) { //we have success
                 if (listener != null) {
                     listener.onSuccess();
                 }
             }


         }
    }


}

And then, in you activit/fragment/service/ use something like this:

public class MyActivity extends Activity {


    private void someInstanceMethod() {/ *do your work here */}

    @Override
    public void onCreate(Bundle savedInstanceState) {

          //setup ui, or do whatever you need

          //create MyAsyncTask with proper listener
          MyAsyncTask task = new MyAsyncTask(new MyAsyncTask.MyAsyncTaskListener() {

              @Override
              public void onSuccess() {
                  //call your instance method here
                  someInstanceMethod();

              }

              @Override
              public void onFailure() {
                  //process fail
              }

              @Override
              public void onError() {
                  //process error
              }


          });



    } 

}
Sign up to request clarification or add additional context in comments.

Comments

0

This is one method. I don't know if it is the best one:

Make work function as public static void. Call it from Asynctask onpostexecute as

HomeActivity.Work(); 

Edit: One more way( again not sure if this is the best way):

If you cant make this work, consider putting your asynctask class inside the home activity class

1 Comment

cant make that static. I am using click listeners in the method
0

Well using the FragmentManger findFragmentById() or findFragmentByTag() you can get an instance of the current fragment and call your fragment method.

2 Comments

How can i get the Id in my case?
Try to bring the AsyncTask as inner class inside HomeActivity, make the FragmentManager a class variable, now you have access to fragment manager anywhere you want.
0

Create an interface file

public interface AsynAction
{
  public void Work();
}

Implements AsynAction in HomeActivity

public class HomeActivity extends FragmentActivity implements OnClickListener,AsyncAction {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                FragmentManager fm = getSupportFragmentManager();

                // Create the list fragment and add it as our sole content.
                if (fm.findFragmentById(android.R.id.content) == null) {
                    HomeFragment list = new HomeFragment();
                    fm.beginTransaction().add(android.R.id.content, list).commit();

                }
            }
        public static class HomeFragment extends Fragment {

        webServiceTask = WebServiceTask.getInstance(
                                    getActivity(), Constants.METHOD_NAME_PRODUCTS,
                                    Constants.PRODUCT_NAME, null);
@Override
public  void Work() 
          {
          }
    }
    }

Then make changes in you asynctask to receive asyncAction object as reference

final public class WebServiceTask extends AsyncTask<String, String, String> {
    private WebServiceTask(final AyscAction asycAction,final Activity activity, final String methodName,
                final String productName, final String addInfo[]) {
            super();
            this.activity = activity;
            this.asycAction=asycAction;
            this.methodName = methodName;
            this.productName = productName;
            this.addInfo = addInfo;
        }
public static WebServiceTask getInstance(final AyscAction asycAction,final Activity activity,
            final String methodName, final String productName,
            final String additionalInfo[]) {
        webServiceTask = new WebServiceTask(asycAction,activity, methodName, productName,
                additionalInfo);
        return webServiceTask;
    }
    protected void onPostExecute() {

    // You can call work from here
       if(asynAction!=null)
          asyncAction.Work();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.