界面在活动和片段之间自动变为null。

huangapple go评论99阅读模式
英文:

Interface between activity and fragment becomes null automatically

问题

我有一个包含菜单按钮和两个按钮的活动。我有三个片段Frag_A、Frag_B和Frag_C。默认情况下,我加载活动时使用片段Frag_A。根据在活动中单击不同的按钮,我在这些片段之间切换。

为了让片段访问活动按钮,我创建了一个接口BasicClickListeners,并使活动实现了这个接口。

public interface BasicClickListeners {

    void onMenuClick();

    void goToFragment2();

    void goToFragment3();
}

我在所有片段中实现了两个方法:

public synchronized void registerBasicListener(BasicClickListeners listener) {
    basicClickListeners = listener;
}

private synchronized void unregisterBasicListener() {
    basicClickListeners = null;
}

所以,每当我通过活动启动一个片段时,我会从活动中调用registerBasicListener()方法。在所有片段的onDestroy()方法中,我都调用unregisterBasicListener()。

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterBasicListener();
}

现在,如果我想从片段1跳转到片段2,我会在片段2中使用basiClickListeners.goToFragment2()方法,它可以正常工作。

我不知道为什么有时会在basicClickListeners上出现NPE(空指针异常)。即使片段是活动的,为什么basicClickListeners会变为null呢?而且这并不总是发生,只是偶尔发生。我无法确定出现这个问题的模式。请帮助我解决这个问题。

英文:

I have an activity with a menu button and two buttons. I have three fragments Frag_A, Frag_B, and Frag_C. By default, I am loading the activity with the fragment Frag_A. Based on clicking different buttons in the activity, I switch between the fragments.

For the fragment to access the activity buttons, I have created an interface BasicClickListener and I have made the activity implement the interface.

public interface BasicClickListeners {

    void onMenuClick();

    void goToFragment2();

    void goToFragment3();
} 

I have implemented two methods in all fragments:

public synchronized void registerBasicListener(BasicClickListeners listener) {
    basicClickListeners = listener;
}

private synchronized void unregisterBasicListener() {
    basicClickListeners = null;
}

So, whenever I launch a fragment through the activity, I call registerBasicListener() method from the activity. And on all fragments' onDestroy(), I am calling unregisterBasicListener.

@Override
    public void onDestroy() {
        super.onDestroy();
        unregisterBasicListener();
    }

Now if I wanted to go to fragment 2 from fragment 1, I use basiClickListeners.goToFragment2() from fragment 2, and it works.

I don't know why sometimes I am getting NPE on basicClickListeners. Even though the fragment is active, how can the basicClickListeners become null? And it's not happening always. Only sometimes. I couldn't identify a pattern on why it's occurring. Kindly assist me with this issue.

答案1

得分: 1

你可以在Fragment中使用onAttach()onDetach()重写方法来注册和清除接口监听器。同时,确保该接口在你的活动中被实现。然后,你可以使用mCallback引用来调用goToFragment2()goToFragment3()方法。

YourFragment

public class YourFragment extends Fragment {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (BasicClickListeners) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement BasicClickListeners");
        }
    }
    @Override
    public void onDetach() {
        mCallback = null;
        super.onDetach();
    }
}

YourActivity:

public class MainActivity extends AppCompatActivity 
implements BasicClickListeners {

}
英文:

You can use onAttach() and onDetach() overridden method in Fragment for registering and clearing the interface listeners. Also, make sure that the interface is implemented in your activity. Then, you can use mCallback reference for calling the goToFragment2(), goToFragment3() methods.

YourFragment

public class YourFragment extends Fragment {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (BasicClickListeners) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement BasicClickListeners");
        }
    }
    @Override
    public void onDetach() {
        mCallback = null;
        super.onDetach();
    }
}

YourActivity:

public class MainActivity extends AppCompatActivity 
implements BasicClickListeners {

}

huangapple
  • 本文由 发表于 2020年7月31日 22:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63193421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定