0

After the User has submitted the form details by clicking submit button, I want to switch to a new Fragment when the if condition [ if (success == 1) ] is True. How should I proceed?

Here is my code:

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();
    }
}
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Since CreateNewProduct is a class, I would create a constructor taking the calling Activity as a parameter and save as an instance. In onPostExecute (which runs on main thread), I would then call some method in the activity instance with a result. I know that this seems like an ugly solution, but it works fine. – Pphoenix Jun 11 '14 at 08:08

3 Answers3

0

You should not do that in a background thread, try this instead:

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 {

        }
    }
}

If CreateNewProduct is a class inside an Activity , you can add the fragment as you would add any other fragment:

    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 {

        }
    }

As I just noticed that this AsyncTask most likely is inside a fragment here is another approach:

In your activity:

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

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

And in the AsyncTask onPostExecute:

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

        }
    }
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • Tried to elaborate a bit. The simplest solution would be to have the AsyncTask as a class inside the activity class. This way you can access fragment manager directly. – cYrixmorten Jun 11 '14 at 08:16
0

While that seems like something that would go against the inner working of Android, I think it could be possible using local broadcasts like in the code I posted in this thread: Using LocalBroadcastManager to communicate from Fragment to Activity

It is not advised, though. But I think it makes what you want possible.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

You should define a callback interface (with probably two methods onSuccess() and onFailure()) and let your calling activity or fragment act as a switchboard and change the UI. This is one of the good practices in android.

A small hint you can get from this blog post http://samir-mangroliya.blogspot.in/p/android-seperate-asynctask-class.html .

Happy coding :) !!

Shubhang Malviya
  • 1,525
  • 11
  • 17