Call an activity method from a fragment

Joakim Ericsson

Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create problems in the activity.

New to fragments so I need an easy and pedagogic explanation!

Thanks!

Richard

From fragment to activty:

((YourActivityClassName)getActivity()).yourPublicMethod();

From activity to fragment:

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();

If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call Fragment method from Activity

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

Call dynamic fragment method from activity

From Dev

Call Tabbed Fragment method from Activity

From Dev

How to call method inside the fragment from activity?

From Dev

Call fragment method from activity but view is null

From Dev

Call a method in a drawer fragment from the base activity

From Dev

Call fragment method from activity but view is null

From Dev

How call method from Fragment in Activity?

From Dev

How to call ViewHolder method from Activity/Fragment

From Dev

Call method from Main Activity into Fragment Adapter

From Dev

Call fragment method in an activity?

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Trying to call a fragment method from parent activity but the method is not being resolved

From Dev

how to call a fragment method in an activity

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Call method of interface implements with child fragment From container activity Android

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

I Can't Call Fragment Method from Activity

From Dev

Call activity from fragment in android

From Dev

Call activity from fragment in android

From Dev

how to call a fragment from activity

From Dev

Calling fragment method from activity

From Dev

Fragment method not accessible from Activity

From Dev

Fragment method not accessible from Activity

Related Related

  1. 1

    Call Fragment method from Activity

  2. 2

    Call Fragment method from Activity

  3. 3

    Call method in fragment from another fragment/activity

  4. 4

    Call method in fragment from another fragment/activity

  5. 5

    Call dynamic fragment method from activity

  6. 6

    Call Tabbed Fragment method from Activity

  7. 7

    How to call method inside the fragment from activity?

  8. 8

    Call fragment method from activity but view is null

  9. 9

    Call a method in a drawer fragment from the base activity

  10. 10

    Call fragment method from activity but view is null

  11. 11

    How call method from Fragment in Activity?

  12. 12

    How to call ViewHolder method from Activity/Fragment

  13. 13

    Call method from Main Activity into Fragment Adapter

  14. 14

    Call fragment method in an activity?

  15. 15

    Call method in child fragment in parent fragment from activity

  16. 16

    Call method in child fragment in parent fragment from activity

  17. 17

    Trying to call a fragment method from parent activity but the method is not being resolved

  18. 18

    how to call a fragment method in an activity

  19. 19

    Call method of interface implements with child fragment From container activity Android

  20. 20

    Call method of interface implements with child fragment From container activity Android

  21. 21

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

  22. 22

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

  23. 23

    I Can't Call Fragment Method from Activity

  24. 24

    Call activity from fragment in android

  25. 25

    Call activity from fragment in android

  26. 26

    how to call a fragment from activity

  27. 27

    Calling fragment method from activity

  28. 28

    Fragment method not accessible from Activity

  29. 29

    Fragment method not accessible from Activity

HotTag

Archive