美文网首页
Android Jetpack架构组件之Navigation使用

Android Jetpack架构组件之Navigation使用

作者: VinPin | 来源:发表于2019-03-27 11:24 被阅读0次

    添加依赖

    api "androidx.navigation:navigation-fragment:2.1.0-alpha01"
    

    开始使用

    第一步:创建NavHost一个空容器,用于显示导航图中的目标。

    一般是在Activity的xml布局中显示Fragment。

    <androidx.constraintlayout.widget.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white">
    
        <fragment
                android:id="@+id/fragment_host"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:navGraph="@navigation/nav_graph"
                app:defaultNavHost="true"
                android:name="androidx.navigation.fragment.NavHostFragment"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    android:name属性包含实现的类名NavHost 。固定写法,默认NavHostFragment。
    app:navGraph属性将其NavHostFragment与导航图相关联。
    app:defaultNavHost="true"属性确保您NavHostFragment 拦截系统“后退”按钮。效果等同于

    // avctivity中覆写该方法
    override fun onSupportNavigateUp(): Boolean {
        return Navigation.findNavController(this, R.id.fragment_host).navigateUp()
    }
    

    第二步:创建导航图,一种XML资源,包含一个集中位置中的所有导航相关信息。

    在AndroidStudio中右键res目录->New->Android Resource File->选择Resource type为Navigation。


    image.png

    添加两个Fragment:Demo1Fragment和Demo2Fragment后的navigation如下

    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/nav_graph"
                app:startDestination="@id/demo1Fragment">
    
        <fragment
                android:id="@+id/demo1Fragment"
                android:name="com.vinpin.main.ui.jetpack.navigation.Demo1Fragment"
                tools:layout="@layout/fragment_navigation_demo1"
                android:label="Demo1Fragment">
            <action
                    android:id="@+id/action_demo1Fragment_to_demo2Fragment"
                    app:enterAnim="@anim/slide_in_right"
                    app:exitAnim="@anim/slide_out_left"
                    app:destination="@id/demo2Fragment"
                    app:popExitAnim="@anim/slide_out_right"
                    app:popEnterAnim="@anim/slide_in_left"/>
        </fragment>
        <fragment
                android:id="@+id/demo2Fragment"
                android:name="com.vinpin.main.ui.jetpack.navigation.Demo2Fragment"
                tools:layout="@layout/fragment_navigation_demo2"
                android:label="Demo2Fragment">
            <action
                    android:id="@+id/action_demo2Fragment_to_demo3Fragment"
                    app:enterAnim="@anim/slide_in_right"
                    app:exitAnim="@anim/slide_out_left"
                    app:destination="@id/demo3Fragment"
                    app:popExitAnim="@anim/slide_out_right"
                    app:popEnterAnim="@anim/slide_in_left"/>
        </fragment>
    
    </navigation>
    

    第三步:获取NavController,管理应用程序导航的对象NavHost。

    在第二步中指定app:startDestination="@id/demo1Fragment",那么activity显示时Demo1Fragment会立即显示。那么所有的跳转动作都是从Demo1Fragment开始的。

    // Demo1Fragment中txt_btn按钮点击事件,跳转Demo2Fragment
    txt_btn.setOnClickListener {
        Navigation.findNavController(it).navigate(R.id.action_demo1Fragment_to_demo2Fragment)
    }
    

    Fragment跳转之间的生命周期

    Demo1Fragment 跳转 Demo2Fragment:(先执行Demo2Fragment的生命周期)

    Demo2Fragment: onAttach->onCreate->onCreateView->onActivityCreated->onStart->onResume
    Demo1Fragment: onPause->onStop->onDestroyView
    

    Demo2Fragment 返回 Demo1Fragment:(先执行Demo1Fragment的生命周期)

    Demo1Fragment onCreateView->onActivityCreated->onStart->onResume
    Demo2Fragment onStop->onDestroyView->onDestroy->onDetach
    

    结论:
    A跳转B时,先执行B的onAttach直到onResume,再执行A的onPause直到onDestroyView。
    B返回A时,先执行A的onCreateView直到onResume,再执行B的onStop直到onDetach。

    相关文章

      网友评论

          本文标题:Android Jetpack架构组件之Navigation使用

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