Android NavigationView onDestroy 未调用

标签 android android-fragments android-navigationview event-bus greenrobot-eventbus

我有一个带导航 View 的布局。 View 层次结构解释如下,

Navigation View> A > A1
Navigation View> B

现在,当我在 Fragment A1 中时,如果我通过单击导航 View 中的菜单打开 Fragment B 或 Fragment A,则不会在 Fragment A1 中调用 onStop 或 onDestroyView。

我已经在 Fragment A1 中注册了 green-bot 事件,现在由于没有调用 onStop,事件永远不会取消注册。因此,当我再次回到 A1 时,它正在注册一个新事件,我得到了所有事件两次。

我该如何解决这个问题?

谢谢

最佳答案

FragmentonAttachonDetach 方法怎么样。在主机 Activity 中添加、删除或替换 fragment 时调用这些方法。

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
       //register green-bot event here
    }

@Override
    public void onDetach() {
       //un-register green-bot here
       super.onDetach();
    }

关于Android NavigationView onDestroy 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808733/

相关文章:

android - onCreateOptionsMenu() 在 Fragment 中调用了两次

android - ViewPager 安卓 :NullPointerException: Attempt to invoke virtual method

android - 如何使用 Expandable 关闭 NavigationView 中的组菜单

android - 调用 Android 的 Sqlite 数据库的 close() 后会发生什么?

android - 我应该如何将元素添加到自定义相机 android 应用程序?

Android:一个对象的360度 View

android - PlaceAutocompleteFragment - 不能将 null 转换为非 null 类型 (Kotlin)

android - 如何更改 NavigationView 中的标题高度?

android - 如何(以及在​​哪里)修复 android.os.BadParcelableException : ClassNotFoundException when unmarshalling: RecyclerView$SavedState?

c# - 将c#解密方法转换为android