How to use interface to communicate between fragment and activity?

痴心易碎 提交于 2021-01-05 07:32:42

问题


I simply want to call a Fragment method from my MainActivity.

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.

@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. I tried it like this:

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

But here I get an nullPointerException:

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference Which reffers to the Toast message.

How do I call the method from my Interface in my MainActivity without getting an NullPointerException?

Thanks


回答1:


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 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 below

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

        }
        });



回答2:


If you want to access your method from Activity to 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.

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



回答3:


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:

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


来源:https://stackoverflow.com/questions/65117300/how-to-use-interface-to-communicate-between-fragment-and-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!