简体   繁体   English

从无关类中调用片段中的方法

[英]Call Method in Fragment from Unrelated Class

Tricky question here, but I will try and be clear. 这里有棘手的问题,但我会尽力清楚。

Class A: extends FragmentActivity and has the FragmentManager A类: extends FragmentActivity并具有FragmentManager

Class B: extends Fragment and has the fragment's UI B类: extends Fragment并拥有片段的UI

Class C: Unrelated Class C类:无关类

    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? 我想要做的是从C类调用一个位于B类的方法。任何想法我怎么能这样做?

A solution would be a way to run this code from Class C: 解决方案是从C类运行此代码的方法:

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

This does not work (it compiles, but the if statement is always false): 这不起作用(它编译,但if语句始终为false):

    if (A.getCurrentFragment() instanceof B) {
        B fragment = (B) A.getCurrentFragment();
        fragment.methodToBeCalled();
    }

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. 将FragmentActivity的上下文传递给C类,使用此上下文可以获取片段管理器,然后您可以使用片段ID或标记找到片段。

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();

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")
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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM