简体   繁体   English

Fragment和Activity之间如何使用接口进行通信?

[英]How to use interface to communicate between fragment and activity?

I simply want to call a Fragment method from my MainActivity.我只想从我的 MainActivity 中调用 Fragment 方法。

So I tried to use an Interface.所以我尝试使用一个接口。

public interface MyInterface {
        void testMethod();
}

In my Fragment (TestFragment.java) I implement the interface and overrite the testMethod method.在我的片段 (TestFragment.java) 中,我实现了接口并覆盖了 testMethod 方法。

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

but now I want to call this method from my MainActivity as soon as the onRewardedVideoCompleted get's called, but I'm not sure how to do it.但现在我想在调用 onRewardedVideoCompleted 后立即从我的 MainActivity 调用此方法,但我不确定该怎么做。 I tried it like this:我这样试过:

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

But here I get an nullPointerException:但在这里我得到一个 nullPointerException:

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference Which reffers to the Toast message.尝试在引用 Toast 消息的 null object 引用上调用虚拟方法“java.lang.String android.content.Context.getPackageName()”。

How do I call the method from my Interface in my MainActivity without getting an NullPointerException?如何在不出现 NullPointerException 的情况下从我的 MainActivity 中的接口调用方法?

Thanks谢谢

You need to create the interface for it like below您需要为其创建界面,如下所示

public interface FilterValuePassInterface {

    public void onSelectedFilterValue(String name);
}

Fragment class should look like below片段 class 应该如下所示

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) {

    }
}

And inside the Activity class, you need to create the method setOnDataListener and initialise the fragment like belowActivity class 中,您需要创建方法setOnDataListener并初始化如下片段

 MyFragment myFragment;
    public void setOnDataListener(MyFragment myFragment) {

    this.myFragment = myFragment;

    }

Again inside the activity you can send the data from any click or event, you just need to call this method from the activity to transfer the data in fragment like below再次在活动中,您可以从任何点击或事件发送数据,您只需要从活动中调用此方法来传输片段中的数据,如下所示

    YOUR_CLICK.setOnClickListener(new OnClickListener() {

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

        }
        });

If you want to access your method from Activity to Fragment.如果你想访问你的方法从 Activity 到 Fragment。 You do not need any interface.您不需要任何界面。 You just need to call the method from the fragment instance.您只需要从片段实例中调用该方法。 However, if you want access Activity's method, you may use the interface.但是,如果你想访问 Activity 的方法,你可以使用接口。

public interface MyInterface {
        void testMethod();
}

And in your activity,在你的活动中,

class MyActivity implements MyInterface{
void testMethod(){
}
}

in your fragment,在你的片段中,

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

class MyActivity implements MyInterface{
    public void testMethod(){
    }
}

Inside your main(), you can create a new object of MyActivity like this which will allow you to access the method:在您的 main() 中,您可以像这样创建一个新的 MyActivity object,这将允许您访问该方法:

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

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

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