How to call Activity methods from inside a Fragment

thomas taylor

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);
}
Fuat ÖZERGİL

You can call like this;

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call method inside the fragment from activity?

From Dev

Call Activity's methods from fragment

From Dev

how to call a fragment from activity

From Dev

Android - How to call a fragment from inside a fragment?

From Dev

How to change the activity background from fragment inside it

From Dev

How to call a fragment from another activity

From Dev

How call method from Fragment in Activity?

From Dev

How to call ViewHolder method from Activity/Fragment

From Dev

How can activity call a method inside fragment created dynamically

From Dev

Activity not launched from inside fragment

From Java

Call an activity method from a fragment

From Dev

Call activity from fragment in android

From Dev

Call Fragment method from Activity

From Dev

Call activity from fragment in android

From Dev

Call Fragment method from Activity

From Dev

Call method in fragment from another fragment/activity

From Dev

Call method in fragment from another fragment/activity

From Dev

How to access a particular View inside a Fragment from the parent Activity?

From Dev

Android; How to return a method for value from an activity inside a fragment?

From Dev

how to call a fragment method in an activity

From Dev

Inside Activity, How to pause a for loop to call a fragment and after button click on fragment then resume the loop to start again

From Dev

Android: How to call Fragment class from activity using Intent

From Dev

Android How to call Fragment from my Main Activity

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

How to call a method exist in ActionBar Tab Fragment from main activity

From Dev

How can i call a method in my fragment from my activity?

From Dev

How to call an activity's methods from a separate class

From Dev

Accessing fragment methods from parent Activity

From Dev

How to call a method from a different class inside the main activity?

Related Related

  1. 1

    How to call method inside the fragment from activity?

  2. 2

    Call Activity's methods from fragment

  3. 3

    how to call a fragment from activity

  4. 4

    Android - How to call a fragment from inside a fragment?

  5. 5

    How to change the activity background from fragment inside it

  6. 6

    How to call a fragment from another activity

  7. 7

    How call method from Fragment in Activity?

  8. 8

    How to call ViewHolder method from Activity/Fragment

  9. 9

    How can activity call a method inside fragment created dynamically

  10. 10

    Activity not launched from inside fragment

  11. 11

    Call an activity method from a fragment

  12. 12

    Call activity from fragment in android

  13. 13

    Call Fragment method from Activity

  14. 14

    Call activity from fragment in android

  15. 15

    Call Fragment method from Activity

  16. 16

    Call method in fragment from another fragment/activity

  17. 17

    Call method in fragment from another fragment/activity

  18. 18

    How to access a particular View inside a Fragment from the parent Activity?

  19. 19

    Android; How to return a method for value from an activity inside a fragment?

  20. 20

    how to call a fragment method in an activity

  21. 21

    Inside Activity, How to pause a for loop to call a fragment and after button click on fragment then resume the loop to start again

  22. 22

    Android: How to call Fragment class from activity using Intent

  23. 23

    Android How to call Fragment from my Main Activity

  24. 24

    How to call fragment from activity without using fragmentactivity?

  25. 25

    How to call a method exist in ActionBar Tab Fragment from main activity

  26. 26

    How can i call a method in my fragment from my activity?

  27. 27

    How to call an activity's methods from a separate class

  28. 28

    Accessing fragment methods from parent Activity

  29. 29

    How to call a method from a different class inside the main activity?

HotTag

Archive