为什么我不能调用一个方法来从一个片段父活动? [英] Why can't i call a method to the parent activity from a fragment?

查看:92
本文介绍了为什么我不能调用一个方法来从一个片段父活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想从包含该片段的FragmentActivity调用一个方法的片段。我在FragmentTabs(延伸FragmentActivity),设置在所述片段的特定事件的lastTab片段中定义的方法。 出于某种原因,getActivity()。SomeUserDefinedActivity()不能得到解决。

下面是code说明我的问题:

  @覆盖
公共无效onActivityCreated(包savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    testButton =(按钮)getView()findViewById(R.id.button1)。
    testButton.setOnClickListener(新OnClickListener()
    {
        @覆盖
        公共无效的onClick(视图v)
        {
            Log.i(FragmentTest,Button1的点击的);


            TestFragment2 F2 =新TestFragment2();
            FragmentTransaction英尺= getActivity()getSupportFragmentManager()的BeginTransaction()。
            ft.replace(R.id.realtabcontent,F2);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(空);
            ft.commit();
            //这是我想打电话给FragmentTabs方法SetLastFragment,但我不能访问它。
            getActivity()SetLastFragment(F2)。

        }
    });
}
 

这是我想在FragmentTabs调用的方法:

 公共无效SetLastFragment(片f)
{
    mTabManager.SetLastTabFragment(F);
}
 

解决方案

getActivity()返回只是一个活动实例。这可以是你的嵌入片段的任何活动。 SetLastFragment()是你 FragmentTabs 一个特定的活动,命名的方法。并不是所有的活动都有这个方法。

这意味着你必须将它转换为特定的活动类。如果您使用多个工作之外的活动的片段,你也应该检查是否正确的活动时,如通过的instanceof 操作符:

 活性= getActivity();

如果(一个的instanceof FragmentTabs){
    ((FragmentTabs)一).SetLastFragment(F2);
}
 

I have a fragment where I wish to call a method from the FragmentActivity that contains this fragment. I defined a method in FragmentTabs(extends FragmentActivity) that sets the lastTab fragment on a specific event in the fragment. For some reason getActivity().SomeUserDefinedActivity() cannot be resolved.

Here is the code explaining my problem :

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    testButton = (Button)getView().findViewById(R.id.button1);
    testButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            Log.i("FragmentTest", "Button1 Clicked");


            TestFragment2 f2 = new TestFragment2();
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.realtabcontent, f2);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit();
            // This is where I want to call FragmentTabs method SetLastFragment, but I cannot access it.
            getActivity().SetLastFragment(f2);

        }
    });
}

This is the method I wish to call in FragmentTabs:

    public void SetLastFragment(Fragment f)
{
    mTabManager.SetLastTabFragment(f);
}

解决方案

getActivity() returns just an Activity instance. This can be any activity that embeds your fragment. SetLastFragment() is a method of one specific activity of yours, named FragmentTabs. Not all activities have this method.

Which means you have to cast it to your specific activity class. If you use your fragment in multiple activties, you should also check if the correct activity is used, e.g. via the instanceof operator:

Activity a = getActivity();

if(a instanceof FragmentTabs) {
    ((FragmentTabs) a).SetLastFragment(f2);
}

这篇关于为什么我不能调用一个方法来从一个片段父活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆