从片段中调用父活动中的AsyncTask

时间:2014-11-06 12:16:28

标签: java android android-fragments android-asynctask

我有片段寻呼机的活动,我想知道如何从片段调用位于该活动上的asynctask。
AddActivity.java

public class AddActivity extends FragmentActivity {

    ViewPager vp;
    ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        vp = (ViewPager) findViewById(R.id.pager);
        PagerAdapter pa = new FragPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(pa);

    }


    //This void to get All field from the differents fragments and put them into jsonObject
    protected JSONObject createJsonObjectVR () {
        ...
    }

    // Call the Post Method
    public class addVR extends AsyncTask<String, String, String> {
         ...
    }

OtherFragment.java

public class OtherFragment extends Fragment {
    Button btn;

    public OtherFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_other, container, false);
        btn = (Button) view.findViewById(R.id.send);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Here I want to call the createVR asynctask in the parent Activity
            }
        });
        return view;
    }
}

FragPagerAdapter

public class FragPagerAdapter extends FragmentPagerAdapter {

    public FragPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                // First Fragment The main
                return new FirstFragment();
            case 1:
                // Milk (Second) fragment activity
                return new SecondFragment();
            case 2:
                // Housing (Third) fragment activity
                return new HousingFragment();
            case 3:
                //Feeding (fourth) fragment activity
                return new FeedingFragment();
            case 4:
                // Other (Fith the last) fragment activity
                return new OtherFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 5;
    }

}

1 个答案:

答案 0 :(得分:11)

你片段中的

((AddActivity) getActivity()).someMethodToCallAsyncTaskInAddActivity();

你的AddActivity:

public void someMethodToCallAsyncTaskInAddActivity() {

       new AsyncTask(someOptions_if_needed).execute();

}

还有一个选项

public static  class ASyncronius extends AsyncTask<Void, Void, Void> 

如果你将它设为静态,你可以从任何地方调用这个异步任务

AddActivity.ASyncronius  abs = new AddActivity.ASyncronius ();
abs.execute()

编辑2014-11-11

如果你在不同的活动中使用片段,那么你应该做shure,那些活动扩展同一个包含异步方法的主类。

class ParentActivity extends Activity {

someMethodToCallAsyncTaskInParentActivity(){
***new async execute***
} 

  public class async extends AsyncTask<void, void, void>{
  ***Some stuff***
 }
}

然后:

class FirstActivity extends ParentActivity{

}

class SecondActivity extends ParentActivity{

}
片段中的

((ParentActivity)getActivity()).someMethodToCallAsyncTaskInParentActivity();

或者如果您不需要在其他活动中使用异步任务,那么只需执行以下操作:

try{
 ((FirstActivity)getActivity()).someMethodToCallAsyncTaskInParentActivity();
} catch( Exception e) {
   // this is not needed activity
}