2013-12-13 43 views
1

我有一個類‘HomeActivity’,這是如下:問題與調用片段類的方法

 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() {} 
    } 
    } 

我有另一個類WebServiceTask,這是如下:

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? 
    } 

我的問題是我怎麼能從onPostExecute()在HomeFragment類中調用work()方法。

+0

工作方法在HomeFragment類中。另外我有活動變量。 – Rookie

+0

哦,我沒看到。 – TheFlash

回答

1

我會建議爲你做一個監聽器任務,並在後期執行中調用它的方法。它會讓你在任務完成後有更多靈活性和控制權。下面是示例代碼,我會用:

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(); 
       } 
      } 


     } 
    } 


} 

,然後在你activit /片段/服務/使用這樣的:

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 
       } 


      }); 



    } 

} 
0

這是一種方法。我不知道它是否是最好的:

使公共工作職能靜態 void。從的AsyncTask叫它onpostexecute作爲

HomeActivity.Work(); 

編輯: 另一種方式(同樣不知道這是最好的方法)

如果你不能完成這項工作,可以考慮把你的AsyncTask類裏面家庭活動類

+0

不能使靜態。我在方法 – Rookie

+0

中使用點擊偵聽器檢查我的編輯並嘗試 – SoulRayder

0

那麼使用FragmentMangerfindFragmentById()findFragmentByTag()你可以得到一個當前片段的實例並調用你的片段方法。

+0

我如何獲得我的情況下的Id? – Rookie

+0

嘗試將AsyncTask作爲HomeActivity中的內部類來使用,使FragmentManager成爲一個類變量,現在您可以在任意位置訪問片段管理器。 –

0

創建接口文件

public interface AsynAction 
{ 
    public void Work(); 
} 

器具AsAction 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() 
      { 
      } 
    } 
    } 

然後在你的asynctask中修改asyncAction對象作爲參考

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(); 
    }