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

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

问题描述

我使用下面的方法片段之间通过显示/隐藏进行切换(在我的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;
    }

什么我不清楚的是该片段生命周期的方法,当我显示或隐藏它被称为?(由于是作为OnShow中()或onHide()我不是很确定没有方法,例如什么土色)。我想在显示和隐藏特定的片段做的具体行动。

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 tuse). I want to do specific actions upon showing and hiding a specific Fragment.

推荐答案

类似的活动周期,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.

根据您的布局的Andr​​oid可以调用 ONSTART()甚至,当你的片段尚未出现,但它属于一个可见的父容器。例如,这是有效的 android.support.v4.view.ViewPager 这就需要你重写<一个href="http://developer.android.com/reference/android/app/Fragment.html#setUserVisibleHint%28boolean%29"><$c$c>Fragment.setUserVisibleHint()方法。在任何情况下,如果您需要注册/注销BroadcastReceivers或其他听众,你可以放心地使用 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.

注意:有些片段容器可以保持不可见的碎片开始。为了处理这种情况,你可以覆盖<一个href="http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged%28boolean%29"><$c$c>Fragment.onHiddenChanged(boolean隐藏) 。根据<一href="http://developer.android.com/reference/android/app/Fragment.html#isHidden%28%29">documentation,一个片段必须既开始和可见(不隐藏)的,是对用户可见。

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天全站免登陆