0

Activity method of fragment call as follows:

MyActivity method = (MyActivity) getActivity (); 
method.MyMethod ("string");

Here's how it looks in the Activity:

public void MyMethod (String var) 
{         
    Toast.makeText (MyActivity.this,                  
        var,Toast.LENGTH_SHORT).show(); 
} 

Question. How to call a method on a Fragment of Activity? (Ie, reverse)

Nex
  • 5
  • 3
  • Duplicate of http://stackoverflow.com/questions/10903077/calling-a-fragment-method-from-a-parent-activity ? – mrroboaat Aug 09 '14 at 20:25

3 Answers3

0

You would need to instantiate the fragment (get an instance of the fragment) and then call the method.

Evan
  • 287
  • 4
  • 14
  • I use SectionsPagerAdapter. when you select a specific section of a fragment is created: return new LoadObjects (); but if I do this: LoadObjects frag = new LoadObjects (); FragmentTransaction transaction = getFragmentManager (). BeginTransaction (); transaction.add (R.id.fragment_container, frag, "listFragment"); transaction.commit (); return frag; application is closed – Nex Aug 09 '14 at 20:39
0

Use the Activity's FragmentManager to get the active instance of your fragment, and continue as usual.

For example:

MyFragment.java:

public void FragmentMethod() {
    doSomethingInFragment();
}

Activity.java:

public void onCreate() {
    FragmentManager fragManager = getSupportFragmentManager();
    MyFragment fragment = (MyFragment)fragManager.getFragments().get(0);
    fragment.FragmentMethod();
}

You'll notice that I retrieved the instance of the fragment using getFragments().get(0), which refers to the first fragment in your FragmentManager's back stack. If you only have one fragment this is OK, otherwise you can use the getFragment(Bundle, String) method.

mittelmania
  • 3,393
  • 4
  • 23
  • 48
  • If you're using the v4 support library, try calling getSupportFragmentManager() instead – mittelmania Aug 09 '14 at 20:38
  • OK, that means that you're using fragments from the regular API. I highly recommend using the v4 fragments as they are supported by more devices. Get the **android-support-v4.jar** library file, place it in your libs directory and replace all the fragment references from android.app to **android.support.v4.app**. This is generally recommended and will also enable the solution to work :) – mittelmania Aug 10 '14 at 07:59
0

If I understood you correctly why not just create public method in fragment and just call it?

public class MyFragment extends Fragment{
...
       public void myMethod (String var){         
             Toast.makeText (getActivity(),var,Toast.LENGTH_SHORT).show(); 
       } 
}

And in your activity:

MyFragment fragment = new MyFragment();
...
getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.content_frame_layout, fragment);
fragment.myMethod("your string");
Roman
  • 166
  • 1
  • 7
  • Have you got **android-support-v4.jar** in **libs** folder? – Roman Aug 09 '14 at 20:56
  • If not, you need to download it and just put it into **libs** folder. Try to download the latest **android-support-v4.jar** with the Android SDK Manager and then copy the jar from `/extras/android/support/v4/android-support-v4.jar` – Roman Aug 09 '14 at 21:05