片段生命周期 - 在显示/隐藏时调用哪个方法? [英] Fragment lifecycle - which method is called upon show / hide?

查看:29
本文介绍了片段生命周期 - 在显示/隐藏时调用哪个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法通过显示/隐藏片段(在我的 NavigationDrawer 中)在它们之间切换.

I am using the following method to switch between Fragments (in my NavigationDrawer) by showing / hiding them.

protected void showFragment(int container, Fragment fragment, String tag, String lastTag, boolean addToBackStack ) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if ( lastTag != null && !lastTag.equals("")) {
            Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
            if ( lastFragment != null ) {
                transaction.hide( lastFragment );
            }
        }

        if ( fragment.isAdded() ) {
            transaction.show( fragment );
        }
        else {
            transaction.add( container, fragment, tag );
        }

        if ( addToBackStack ) {
            transaction.addToBackStack( tag );
        }

        transaction.commit();

        // set the active tag
        activeFragTag = tag;
    }

我不清楚的是在显示或隐藏 Fragments 生命周期的哪个方法时会调用它?(因为没有 onShow() 或 onHide() 之类的方法,我不太确定使用什么).我想在显示和隐藏某个 Fragment 时执行特定操作.

What I am unclear about is which method of the Fragments lifecycle is called when I show or hide it? (since there is no method such as onShow() or onHide() im not quite sure what to use). I want to perform specific actions upon showing and hiding a certain Fragment.

推荐答案

类似于 Activity 生命周期,Android 调用 onStart() 当片段变为可见时.onStop() 通常在片段不可见时调用,但也可以稍后调用.

Similar to activity lifecycle, Android calls onStart() when fragment becomes visible. onStop() is normally called when fragment becomes invisible, but it can also be called later in time.

根据你的布局 Android 可以调用 onStart() 即使你的 Fragment 还不可见,但它属于一个可见的父容器.例如,这对 android.support.v4.view.ViewPager 有效,它要求您覆盖 Fragment.setUserVisibleHint() 方法.在任何情况下,如果您需要注册/取消注册 BroadcastReceiver 或其他侦听器,您可以安全地使用 onStart()onStop() 方法,因为它们将始终被调用.

Depending on your layout Android can call onStart() even, when your Fragment is not yet visible, but it belongs to a visible parent container. For instance, this is valid for android.support.v4.view.ViewPager which requires you to override Fragment.setUserVisibleHint() method. In any case, if you need to register/unregister BroadcastReceivers or other listeners, you can safely use onStart() and onStop() methods because those will be called always.

注意:一些片段容器可以保持不可见的片段启动.要处理这种情况,您可以覆盖 Fragment.onHiddenChanged(布尔隐藏).根据文档,片段必须是开始和可见(非隐藏),对用户可见.

Note: Some fragment containers can keep invisible fragments started. To handle this situation you can override Fragment.onHiddenChanged(boolean hidden). According to the documentation, a fragment must be both started and visible (not hidden), to be visible to the user.

更新:如果您使用 android.support.v4.widget.DrawerLayout,那么即使抽屉打开,抽屉下方的片段也会保持启动和可见.在这种情况下,您需要使用 DrawerLayout.setDrawerListener() 并监听 onDrawerClosed()onDrawerOpened() 回调.

Update: If you use android.support.v4.widget.DrawerLayout then a fragment below the drawer stays started and visible even when drawer is open. In this case you need to use DrawerLayout.setDrawerListener() and listen for onDrawerClosed() and onDrawerOpened() callbacks.

这篇关于片段生命周期 - 在显示/隐藏时调用哪个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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