Call dynamic fragment method from activity

abdfahim

I have different Fragments loaded in my activity dynamically by code below

Fragment fragment = null;
    switch (pos) {
        case 1:
            fragment = new Fragment1();
            break;
        case 2:
            fragment = new Fragment2();
            break;
        default:
            break;
    }
if(fragment != null){
    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.frg_container, fragment, "frag_"+pos).commit();
    ITEMPOS = pos;
}

Now if I want to call "fragMethod" in one of my fragment, how can I do that? I tried below, but it is not recognizing "fragMethod" unless I put class in front. Now I can't hard code the class as it varies (Fragment1 or Fragment 2).

FragmentManager mn = getFragmentManager();
Fragment ft = mn.findFragmentByTag("frag_"+ITEMPOS);
ft.fragMethod(scanResult.toString());

UPDATE: Thanks to Mike M., I learned to use interface :). Here how it is

Activity Class:

public class navContainer extends Activity {
    public interface IFragment
    {
        public void fragMethod(String arg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {
          FragmentManager mn = getFragmentManager();
          Fragment ft = mn.findFragmentByTag("frag_"+ITEMPOS);
          ((IFragment) ft).populateFromBarcode(scanResult.toString());
      }

    }
}

Fragment Class:

public class Fragment1 extends Fragment implements IFragment{
    @Override
    public void populateFromBarcode(String isbn){
        System.out.println(isbn);
    }
}
Mike M.

Use an interface.

public interface IFragment
{
    public void fragMethod(String arg);
}

public class Fragment1 implements IFragment {
    public void fragMethod(String arg) {

    }
    ...
}

public class Fragment2 implements IFragment {
    public void fragMethod(String arg) {

    }
    ...     
}
...
...
FragmentManager mn = getFragmentManager();
Fragment ft = mn.findFragmentByTag("frag_"+ITEMPOS);
((IFragment) ft).fragMethod(scanResult.toString());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call an activity method from a fragment

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 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 an activity method from a fragment

  2. 2

    Call Fragment method from Activity

  3. 3

    Call Fragment method from Activity

  4. 4

    Call method in fragment from another fragment/activity

  5. 5

    Call method in fragment from another fragment/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