如何使用接口在片段和活动之间进行通信?

Posted

技术标签:

【中文标题】如何使用接口在片段和活动之间进行通信?【英文标题】:How to use interface to communicate between fragment and activity? 【发布时间】:2021-03-14 22:57:18 【问题描述】:

我只是想从我的 MainActivity 中调用 Fragment 方法。

所以我尝试使用接口。

public interface MyInterface 
        void testMethod();

在我的 Fragment (TestFragment.java) 中,我实现了接口并覆盖了 testMethod 方法。

@Override
public void testMethod() 
    Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();

但现在我想在调用 onRewardedVideoCompleted get 后立即从 MainActivity 调用此方法,但我不知道该怎么做。 我试过这样:

MyInterface myInterface = new TestFragment();
myInterface.testMethod();

但在这里我得到一个 nullPointerException:

尝试调用虚拟方法'java.lang.String android.content.Context.getPackageName()' 在空对象引用上 指的是 Toast 消息。

如何在我的 MainActivity 中从我的接口调用方法而不会收到 NullPointerException?

谢谢

【问题讨论】:

您创建了一个新片段,但未将其附加到任何活动。要显示 Toast,您需要一个上下文(在本例中是片段已附加到的活动)。因为活动是null,所以应用程序崩溃了。您需要使用 FragmentManager 的 add/replace 方法将 Fragment 附加到一个 Activity,然后直接调用 Fragment 的方法。这种情况下不需要使用接口。 @SonTruong 我必须使用一个接口,因为我有一个管理所有子片段的 TabFragment。因此,在我的 MainActivity 中,我使用 FragmentManager 将 ViewLayout 替换为 TabFragment。 TabFragment 包含 2 个片段,我想在其中一个片段中调用一个方法,而不是从 TabFragment 调用。所以这种情况下是不能使用 FragmentManager 的吧? Fragment 由 Activity 管理,子 Fragment 由 Parent Fragment 管理。如果你想从一个activity调用一个子fragment的方法,那么你从activity调用fragment上的一个方法,根据action/data,fragment会决定调用子fragment的对应方法。当您想从子片段调用父片段的方法或从片段调用活动的方法时,接口将很有用。因为它会增加activity/fragment之间的解耦,让fragment更加可重用。 【参考方案1】:

您需要为它创建如下界面

public interface FilterValuePassInterface 

    public void onSelectedFilterValue(String name);

片段类应该如下所示

class MyFragment extends Fragment implements FilterValuePassInterface 

   @Override
    public void onAttach(@NonNull Context context) 
        super.onAttach(context);
        try 
            ((YOUR_ACTIVITY) getActivity()).setOnDataListener(this);
         catch (Exception ex) 
            ex.printStackTrace();
        
    


    @Override
    public void onSelectedFilterValue(String name) 

    

Activity 类中,您需要创建方法 setOnDataListener 并初始化片段,如下所示

 MyFragment myFragment;
    public void setOnDataListener(MyFragment myFragment) 

    this.myFragment = myFragment;

    

再次在活动内部,您可以从任何点击或事件发送数据,您只需要从活动中调用此方法来传输片段中的数据,如下所示

    YOUR_CLICK.setOnClickListener(new OnClickListener() 

       public void onClick(View v) 
        // TODO Auto-generated method stub
        myFragment.onSelectedFilterValue("YOUR_MSG");

        
        );

【讨论】:

【参考方案2】:
public interface MyInterface 
    void testMethod();


class MyActivity implements MyInterface
    public void testMethod()
    

在你的 main() 中,你可以像这样创建一个 MyActivity 的新对象,这将允许你访问该方法:

MyActivity example= new MyActivity(); 
example.testMethod(); 

【讨论】:

【参考方案3】:

如果您想从 Activity 访问您的方法到 Fragment。您不需要任何接口。您只需要从片段实例中调用该方法。但是,如果你想访问Activity的方法,你可以使用接口。

public interface MyInterface 
        void testMethod();

在你的活动中,

class MyActivity implements MyInterface
void testMethod()


在你的片段中,

class MyFragment extends Fragment
MyInterface myInterface;
public void onActivityCreated(final Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);
        if (getActivity() instanceof MyActivity) 
            myInterface = (MyInterface) getActivity();
        

【讨论】:

以上是关于如何使用接口在片段和活动之间进行通信?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用接口在两个活动之间进行通信

在片段和活动之间进行通信 - 最佳实践

带 Hilt 的活动片段通信

如何在Activity和Fragment之间进行回调?

在tablayout片段之间进行通信[重复]

与另一个片段通信的片段接口