4

Tricky question here, but I will try and be clear.

Class A: extends FragmentActivity and has the FragmentManager

Class B: extends Fragment and has the fragment's UI

Class C: Unrelated Class

    public class A extends FragmentActivity{
        Fragment fragment = new FragmentActivity();
    }

    public class B extends Fragment{
        public void methodToBeCalled(){
             //do something
}
    }

    public class C{
        //call B's methodToBeCalled() from here
        //note it is not static
    }

What I want to do is call a method which is located in Class B from Class C. Any ideas how I could go about doing this?

A solution would be a way to run this code from Class C:

B fragment = (B) getFragmentManager().findFragmentById(R.id.b);

This does not work (it compiles, but the if statement is always false):

    if (A.getCurrentFragment() instanceof B) {
        B fragment = (B) A.getCurrentFragment();
        fragment.methodToBeCalled();
    }
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Can you post an [SSCCE](http://sscce.org) that clearly demonstrates your problem? – Taylor Hx Dec 19 '13 at 03:43
  • sure lemme put that together for u – Evorlor Dec 19 '13 at 03:43
  • Calling a method shouldn't be a problem. Are you perhaps wondering how to get the currently displayed instance of the Fragment? – Dave Dec 19 '13 at 03:45
  • Yes. I believe that may be my question. (Sorry I am new to fragments.) If I could access the currently displayed fragment from the unrelated class, that would solve my problem. i updated code up top. is that what u mean dave? – Evorlor Dec 19 '13 at 03:47

3 Answers3

0

Pass the context of FragmentActivity to the Class C, using this context you can get the fragment manager and then you can find the fragment using the fragment id or tag.

FragmentManager fm = context.getSupportFragmentManager();
//tag should same as what you gave while adding the fragment
FragmentB fb=(FragmentB)fm.findFragmentByTag("tag"); 
// call public methods of FragmentB
fb.CallFromClassC();
Raneez Ahmed
  • 3,808
  • 3
  • 35
  • 58
  • thank you. any chance you can add a few more comments? I am new to fragments. What am I calling getSupportFragmentManager() on? What is the Fragment's tag? – Evorlor Dec 24 '13 at 16:05
  • You can use the tag to call the fragment later with FragmentManager. @Evorlor – Luis Pena Feb 15 '14 at 02:44
0

Use
FragmentTransaction.add(int containerViewId, Fragment fragment, String tag)
or
FragmentTransaction.replace (int containerViewId, Fragment fragment, String tag)
to set tag for a fragment

Then check fragment instance by tag:

A.getCurrentFragment().getTag().equals("btag")
Ivan
  • 703
  • 5
  • 9
0
Step 1 : if class using AsyncTask:

Create a constructor into non-fragment class and parse Fragment Manager:

Step 2:

FragmentManager mFragmentManager;

    // create a constructor
    public NetworkMaster(Activity mMasterActivity){

        this.mFragmentManager = mMasterActivity.getFragmentManager();

    }// end constructor NetworkMaster

Step 3:

protected void onPostExecute(String result) {

            try {

                //ID should same as what you gave while adding the fragment
                SummaryFragment oSummaryFragment = (SummaryFragment)mFragmentManager.findFragmentById(R.id.content_frame); 

                // call public methods of SummaryFragment
                oSummaryFragment.responseFromSummaryUser(result);               

            } catch (Exception e) {
                e.printStackTrace();
            }

        }// end onPostExecute

Step 4: Class the non-fragment class from Fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

NetworkMaster mNetworkMaster = new NetworkMaster(getActivity());
mNetworkMaster.runUserSummaryAsync();

}
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51