我想在AsyncTask执行期间启动一个新的Fragment

时间:2014-06-11 08:01:42

标签: java android android-fragments android-asynctask

用户通过点击提交按钮提交表单详细信息后,我想在if条件[ if (success == 1) ]True时切换到新的片段。我该怎么办?

这是我的代码:

class CreateNewProduct extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Generating Coupon. Please Wait... ");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        String fname = inputFname.getText().toString();
        String lname = inputLname.getText().toString();
        String contact = inputContact.getText().toString();
        String email = inputEmail.getText().toString();
        String eventname = inputEventname.getText().toString();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fname", fname));
        params.add(new BasicNameValuePair("lname", lname));
        params.add(new BasicNameValuePair("contact", contact));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("eventname", eventname));
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

         try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // START A NEW FRAGMENT 

                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        return null;
    }

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

3 个答案:

答案 0 :(得分:0)

你不应该在后台线程中这样做,而是试试这个:

class CreateNewProduct extends AsyncTask<String, String, Integer> {

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Generating Coupon. Please Wait... ");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        String fname = inputFname.getText().toString();
        String lname = inputLname.getText().toString();
        String contact = inputContact.getText().toString();
        String email = inputEmail.getText().toString();
        String eventname = inputEventname.getText().toString();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fname", fname));
        params.add(new BasicNameValuePair("lname", lname));
        params.add(new BasicNameValuePair("contact", contact));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("eventname", eventname));
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

        try {
               int success = json.getInt(TAG_SUCCESS);
               return success;
        } catch (JSONException e) {
                e.printStackTrace();
        }
        return -1;
    }

    protected void onPostExecute(Integer success) {
        pDialog.dismiss();
        if (success == 1) {
            // START A NEW FRAGMENT          
        } else {

        }
    }
}

如果CreateNewProduct是Activity中的一个类,您可以像添加任何其他片段一样添加片段:

    protected void onPostExecute(Integer success) {
        pDialog.dismiss();
        if (success == 1) {
            // START A NEW FRAGMENT     
            FragmentManager fragmentManager = getFragmentManager()
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            ExampleFragment fragment = new ExampleFragment();
            fragmentTransaction.add(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        } else {

        }
    }

正如我刚才注意到,这个AsyncTask很可能是在片段里面,这是另一种方法:

在您的活动中:

public void addCoupon() {
    FragmentManager fragmentManager = getFragmentManager()
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    ExampleFragment fragment = new ExampleFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}

在AsyncTask onPostExecute中:

    protected void onPostExecute(Integer success) {
        pDialog.dismiss();
        if (success == 1) {
            // START A NEW FRAGMENT     
            ((MainActivity)getActivity()).addCoupon();
        } else {

        }
    }

答案 1 :(得分:0)

虽然这似乎与Android的内部工作相悖,但我认为可以使用我在此主题中发布的代码中的本地广播:Using LocalBroadcastManager to communicate from Fragment to Activity

但是,不建议这样做。但我认为它可以满足您的需求。

答案 2 :(得分:0)

您应该定义一个回调接口(可能有两个onSuccess()和onFailure()方法),并让您的调用活动或片段充当交换机并更改UI。这是android中的一个好习惯。

您可以从此博文http://samir-mangroliya.blogspot.in/p/android-seperate-asynctask-class.html获得一些小提示。

快乐编码:) !!