美文网首页
fragment 生命周期加载过程

fragment 生命周期加载过程

作者: 有点健忘 | 来源:发表于2018-07-17 11:24 被阅读6次

分两种情况,一种fragment直接写在xml文件里,一种在java代码里加载,如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:id="@+id/layout_root"
    android:layout_height="match_parent">
<include layout="@layout/inclue_simple_toolbar"/>
    <FrameLayout
        android:id="@+id/layout_fragment2"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>
    <fragment
        android:id="@+id/test222"
        android:name="com.charliesong.wanandroid.setting.TestFragment"
        tools:layout="@layout/simple_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

activity里的log

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println("setContentView 之前====================")
        setContentView(R.layout.activity_test)
        setToolbarTitle("测试页面")
        println("setContentView 之后=====================")
        supportFragmentManager.apply {
            findFragmentById(R.id.layout_fragment2)?:beginTransaction().replace(R.id.layout_fragment2,TestFragment2()).commitAllowingStateLoss()
        }
        println("replace fragment2 commit=====================")
    }

    override fun onStart() {
        super.onStart()
        println("activity onStart====================")
    }

    override fun onResume() {
        super.onResume()
        println("activity onResume================")
    }

fragment就不写了,都是这样的println("onAttach2=====================")
fragment2里多了个数字2,
现在看下打印的结果。
根据图片来简单分下
1~通过xml加载的fragment,可以看到在activity里setcontentview的时候,就开始加载了。
onAttach=====================
onCreate=====================
onCreateView================
onViewCreated
然后等activity的oncreate方法执行完毕的时候执行onActivityCreated方法。
2~通过java代码添加的fragment
这种生命周期都是在activity的oncreate执行完以后才执行的

3~fragment的onstart方法是优先于activity的。


image.png

后退的过程如下


image.png

另外需要注意的地方

对于通过xml加载的fragment,如下

<fragment
        android:id="@+id/test222"
        android:name="com.charliesong.wanandroid.setting.TestFragment"
        tools:layout="@layout/simple_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
//simple_recyclerview.xml如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swf_simple"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_simple"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

这种,普通情况下,你在fragment或者activity里,通过找id =swf_simple,你会发现结果都是空的,反而通过test222找到的才是需要的SwipeRefreshLayout 这个控件。

但是在kotlin代码里,如果你重写fragment的onViewCreated方法,在里边使用一下swf_simple,那么后边就可以继续用这个swf_simple了,你打印下它的值,可以看到它指向的就是test222,如下图

println("2================$swf_simple")
2================android.support.v4.widget.SwipeRefreshLayout{57dd65d V.ED..... ........ 0,228-1024,768 #7f0800f0 app:id/test222}

几个可能用到的方法

可见性,hidden(),show()调用以后这里会走的

    /**
     * Called when the hidden state (as returned by {@link #isHidden()} of
     * the fragment has changed.  Fragments start out not hidden; this will
     * be called whenever the fragment changes state from that.
     * @param hidden True if the fragment is now hidden, false otherwise.
     */
    public void onHiddenChanged(boolean hidden) {
    }

另外一种是用了viewpager加载fragment,用到了FragmentPagerAdapter
那么可以用下边两种方法判断,随便哪个都可以

    public void setMenuVisibility(boolean menuVisible)

 public void setUserVisibleHint(boolean isVisibleToUser) 

相关文章

网友评论

      本文标题:fragment 生命周期加载过程

      本文链接:https://www.haomeiwen.com/subject/muafpftx.html