将接口从 Activity 传递到 Fragment

锡安湾

我正在尝试将信息从片段发送到主要活动。

我正在尝试var interfaceName从主要活动中设置一个片段。

我创建var menuInterface: MenuInterface并尝试将其设置为onNavigationItemSelected使用myFragment.menuInterface = this

menuInterface停留空出于某种原因...任何想法,为什么?

onNavigationItemSelected

override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
    when (menuItem.itemId) {
        R.id.feedLayoutId -> {
            feedFragment = FeedFragment()
            feedFragment.menuInterface = this
            barTitle.text = "myTitle"
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, feedFragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit()
        }
    }
    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}
普拉提克·库马尔

在您的活动中实现 MenuInterface,并从您的代码中删除此行。

feedFragment.menuInterface = this

在你的片段中:

private var menuInterface: MenuInterface? = null

override fun onAttach(context: Context?) {
    super.onAttach(context)
    menuInterface = context as MenuInterface
}

override fun onDetach() {
    menuInterface = null
    super.onDetach()
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将`SharedPreferences` 数据从 Fragment 传递到 Activity

这种通过接口将数据从 Activity 传递到 Fragment 的方法如何在幕后工作?

如何将参数从 Activity 传递到 Fragment 中的函数

将数据从Activity传递到Fragment不会显示任何内容

通过 FragmentPagerAdapter 将数据从 Activity 传递到 ViewPager 的 Fragment

将变量从MainActivity传递到另一个Activity到选项卡Fragment

将数据从Activity传递到Fragment(通过访问来自fragment的Activity),从而给出Class Cast Exception

将ArrayList从Fragment类传递给Activity

onActivityResult 从 Activity 到 Fragment

如何将Activity传递给Fragment中的newInstance

在 Viewpager Tablayout 中将数据从 Activity 传递到 Fragment

将列表从 MainActivity 传递到 Fragment

将 Arraylist 从 Fragment 传递到 RecyclerAdapter (Android)

如何通过解决错误“Activity 无法转换为 Fragment”将 Fragment 传递给类构造函数?

将ArrayList从Activity传递到CustomDialog

将变量从Activity传递到Adapter

将数据从Activity传递到JobService

将Intent数据从Activity传递到Class

将值传递到接口{}

Android在Activity和Fragment之间传递数据

在Kotlin中将值从Activity传递给Fragment

Android存储库模式:如何将额外的数据传递给Fragment / Activity

将字符串从Activity传递回Android中运行的Fragment

如何从 Activity 到 Fragment 进行通信?

从AsyncTask发送数据到Fragment(Not Activity)

从 Main Activity 调用方法到 Fragment Adapter

Kotlin:如何从 Fragment 导航到 Activity

如何将图像上的数据单击从RecycleAdapter传递到Fragment接口,我在MainActivity中使用了该接口来显示数据

使用 Bundle 将 Drawable 从 Fragment 传递到 Dialogfragment