如何判断片段在NavigationDrawer中不可见 [英] How to tell when fragment is not visible in a NavigationDrawer

查看:90
本文介绍了如何判断片段在NavigationDrawer中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图告诉用户何时在我的导航抽屉中选择了另一个片段.我正在尝试使用

I am trying to tell when a user selects a different fragment in my navigation drawer. I was trying to use

override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    super.setUserVisibleHint(isVisibleToUser)
}

我如何在MainActivity中切换片段:

How i switch fragments in my MainActivity:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {

            R.id.nav_camera -> {
                // Handle the camera action
                val fragment: HomeFragment = HomeFragment()
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fragment).commit()

            }
            R.id.nav_manage -> {
                val fragment: SettingFragment = SettingFragment()
                fragmentManager.beginTransaction().replace(R.id.content_main, fragment).commit()

            }
            R.id.nav_share -> {
                onInviteClicked()

            }
            R.id.nav_send -> {

                val emailIntent: Intent = Intent(android.content.Intent.ACTION_SEND)
                emailIntent.type = Constants.FEEDBACK_EMAIL_TYPE

                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                        arrayOf(Constants.FEEDBACK_EMAIL_ADDRESS))

                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        Constants.FEEDBACK_EMAIL_SUBJECT)

                startActivity(Intent.createChooser(
                        emailIntent, Constants.FEEDBACK_TITLE))


            }
        }

        val drawer: DrawerLayout = findViewById(R.id.drawer_layout)
        drawer.closeDrawer(GravityCompat.START)
        return true
    }

但是,这似乎根本没有被调用.例如,在我的NavigationDrawer活动中,它显示了片段A.用户打开导航抽屉并选择片段B.在片段A中没有调用setUserVisibleHint(),因此我的代码可以知道不再显示它.我需要隔离在片段A中的代码来知道何时不显示它,以便可以在某些变量上调用.stop().这与活动中的onPause()相同.

However this does not seem to get called at all. For example, in my NavigationDrawer activity, it shows Fragment A. The user opens the navigation drawer and selects Fragment B. setUserVisibleHint() does not get called in fragment A so my code can know it is no longer shown. I need my code that is isolated in fragment A to know when it is not shown so it can call .stop() on some variables. This is the same use case as onPause() in an activity.

推荐答案

以下是我能想到的几件事...

Here are a few things I can think of...

  1. 使用一致的片段(支持"或本机"),不能同时使用两者.而且,有人说,Support片段是更好的选择(维护得更好).
  2. 确保片段容器没有用XML硬编码.如果要替换片段,则应由代码动态加载初始片段(通常,您将使用id作为R.id. {frameLayoutId}将其加载到FrameLayout中.)
  3. 使用 Frament生命周期事件.替换片段时,onPause会触发,onDetach也会触发.这将告诉您何时您的旧片段不再可见(或不久将不可见).如果没有触发,那么您的代码中还有另一个问题,可能是Fragment类型的混合或XML中的硬编码片段?
  4. 仅在片段寻呼机中使用setUserVisibleHint,或准备手动进行设置. 答案还有更多关于使用setUserVisibleHint.使用寻呼机时,可以一次附加多个片段,因此需要一种额外的方法(有人称它为生命周期事件)来判断一个片段是否真实,真实"可见,因此引入了setUserVisibleHint.
  5. 奖金:如果适合您的应用,请在replace之后调用addToBackStack来使用后退堆栈进行备份.我主要将其添加为他们通常希望在其应用中添加的生命周期项目.代码看起来像这样...

  1. Use a consistent fragment, either Support or Native, not both. And, some say the Support fragment is preferable (better maintained).
  2. Make sure the fragment container is not hard coded in XML. If you intend to replace a fragment, then the initial fragment should be loaded dynamically by your code (you typically will load into a FrameLayout using the id as your R.id.{frameLayoutId}).
  3. Do Use the Frament lifecycle events. onPause fires when you replace a fragment, so does onDetach. That will tell you when your old fragment is no longer visible (or will be invisible shortly). If it does not fire, then you have another issue in your code, possibly mixing of Fragment types, or a hardcoded fragment in XML?
  4. Use setUserVisibleHint only in a fragment pager, or be prepared to set it manually. this answer has a little more to say about the use of setUserVisibleHint. When using a pager, multiple fragments can be attached at once, so an additional means (some call it lifecycle event) was needed to tell if a fragment was "really, truly" visible, hence setUserVisibleHint was introduced.
  5. Bonus: If appropriate for your app, use the back stack for backing up by calling addToBackStack after replace. I add this mainly as an addition lifecycle item one would typically want in their app. The code looks like this...

// to initialize your fragment container
supportFragmentManager
        .beginTransaction()
        .add(R.id.content_fragment, fragment)
        .addToBackStack("blank")
        .commit()

// to update your fragment container
supportFragmentManager
        .beginTransaction()
        .replace(R.id.content_fragment, fragment)
        .addToBackStack("settings")
        .commit()

//in your XML, it can be as simple as adding the FrameLayout below,
// if you start with the Android Studio template for Navigation drawer,
// you can replace the call that includes the "content_main" layout

<!--<include layout="@layout/content_main" /> -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_fragment" />

我希望这会有所帮助.

这篇关于如何判断片段在NavigationDrawer中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆