-1

I've been struggling for a few days trying to get my head around Fragments in Android. At the moment I've created a menu out of buttons that allows the user to switch between Fragments. Each Fragment will then allow the user to to grab a JSON array from a server using a search criteria that is specific to that fragment.

Ideally after each search, the results would remain in memory so that the user could use the menu to switch between the fragments. As the fragments will be using the same methods that currently all sit in the MainActivity, how do I call the methods from MainActivity from inside the fragments?

My code for a method in Activity:

  public ArrayList<Eatery> fillArray() {

    String line;
    Eatery eatery = new Eatery("hello","hello","hello","hello","hello",
            "hello","hello","hello","hello","hello");
    ArrayList<Eatery> eateryList = new ArrayList<>();

    if (getConnection() == true) {
        try {

            URL nameURL = new URL("http://sandbox.kriswelsh.com/hygieneapi/hygiene.php?op=s_name&name=Wok%20This%20Way");
            HttpURLConnection postcodeConnection = (HttpURLConnection) nameURL.openConnection();
            InputStreamReader isr = new InputStreamReader(postcodeConnection.getInputStream());
            BufferedReader nameBF = new BufferedReader(isr);

            while ((line = nameBF.readLine()) != null) {
                JSONArray ja = new JSONArray(line);
                for (int i = 0; i < ja.length(); i++) {

                    JSONObject jo = ja.getJSONObject(i);
                    String id = jo.getString("id");
                    String businessName = jo.getString("BusinessName");
                    String addressLine1 = jo.getString("AddressLine1");
                    String addressLine2 = jo.getString("AddressLine2");
                    String addressLine3 = jo.getString("AddressLine3");
                    String postcode = jo.getString("PostCode");
                    String ratingValue = jo.getString("RatingValue");
                    String ratingDate = jo.getString("RatingDate");
                    String lati = jo.getString("Latitude");
                    String longi = jo.getString("Longitude");
                    eatery.setId(id);
                    eatery.setBusinessName(businessName);
                    eatery.setAddressLine1(addressLine1);
                    eatery.setAddressLine2(addressLine2);
                    eatery.setAddressLine3(addressLine3);
                    eatery.setPostcode(postcode);
                    eatery.setRatingValue(ratingValue);
                    eatery.setRatingDate(ratingDate);
                    eatery.setLati(lati);
                    eatery.setLati(longi);
                    eateryList.add(eatery);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Toast nameToast = Toast.makeText(getApplicationContext(), "Error: No Active Connection", Toast.LENGTH_LONG);
        nameToast.show();
    }

    return (eateryList);
}
thomas taylor
  • 21
  • 1
  • 5
  • 1
    [Fragment-Activity Communication](http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity) – Sevle Feb 25 '16 at 14:56
  • Create an interface to communicate between fragment and activity – Attiq ur Rehman Feb 25 '16 at 14:59
  • While the answers below will work in your case, I highly recommend Marco's approach that is explained here [Call an activity method from a fragment](http://stackoverflow.com/questions/12659747/call-an-activity-method-from-a-fragment). You want to decouple your fragment from a specific activity. (That's the whole point of having fragments!) – Sevle Feb 25 '16 at 15:07
  • Thanks a lot for your help, I did look for existing solutions but for some reason I managed to miss this simple explanation and only got very complicated answers. – thomas taylor Feb 25 '16 at 15:35

3 Answers3

0

Very easy:

if (getActivity() instanceof MainActivity) {    
    ((MainActivity) getActivity()).yourMethod(...);
}
dipdipdip
  • 2,326
  • 1
  • 21
  • 31
0

You can call like this;

((MyActivityClass)getActivity()).methodName();

0

The correct way of communicating is through an interface. Check out this video for getting the job done. The whole series worth the while if you are new to Android development.