Jetpack's Navigation Learning

1. What is Navigation
Navigation is one of the Jetpack architecture components launched by Google. Navigation is responsible for page jumps and value transfers among multiple Fragments in an Activity, while retaining the user experience of switching between multiple Activities.

2. What are the advantages of Navigation?

  • A traditional page is an Activity, and the Activity is heavyweight. The creation of an Activity is very complicated and will destroy a lot of memory resources, and the Fragment is lighter.
  • It can perfectly retain the effect of multiple Activity jumps, and can easily realize the value transfer between pages
  • A large number of Activities are replaced by Fragments, and the maintenance of the life cycle becomes simple, while avoiding memory leaks as much as possible
  • Navigation can configure navigation routing directly in the xml file, and can intuitively see the jump process of multiple pages

3. Start using
1. Add dependencies

dependencies {
    
    
  def nav_version = "2.3.1"
  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"
  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
  implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}

2. Create a navigation configuration file

  1. Create an Activity and two Fragments
  2. Create a new navigation directory under the res directory, and create an xml configuration file under the res directory, and name it my_nav.xml at will
    insert image description here
  3. Open my_nav.xml, and click desgin to enter the design page, click the + sign to add the page that needs to be jumped
    insert image description here
  4. After creating multiple pages, click the small dot on the right side of each page and drag it to the target page to jump to, the flow chart style is realized
    insert image description here
  5. Switch to code mode, there are some configuration statements in the xml file
开始的页面,就是打开Acticity首次加载的是哪个Fragemnt
app:startDestination

<fragment
        android:id="@+id/myFragment1"
        android:name="com.yckj.ui.fragment.Fragment1"
        android:label="fragment_blank"
        //Fragment1对应的自己的xml布局文件
        tools:layout="@layout/fragment_fragment1">
        <action
        	//为当前的跳转action设置id,后面页面跳转的时候需要用到
            android:id="@+id/action_blankFragment_to_blankFragment2"
            //Fragment1跳转到哪个页面上
            app:destination="@id/myFragment2"
            //入场返场动画
            app:enterAnim="@anim/h_fragment_enter"
            app:exitAnim="@anim/h_fragment_exit"
            app:popEnterAnim="@anim/h_fragment_pop_enter"
            app:popExitAnim="@anim/h_fragment_pop_exit" />
    </fragment>

h_fragment_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="150">
    <translate
        android:fromXDelta="10%p"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toXDelta="0" />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1.0" />
</set>

h_fragment_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200">
    <translate
        android:fromXDelta="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="-10%p" />
</set>

h_fragment_pop_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200">
    <translate
        android:fromXDelta="-10%p"
        android:toXDelta="0" />

</set>

h_fragment_pop_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200">
    <translate
        android:fromXDelta="0"
        android:toXDelta="10%p" />

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0" />
</set>
  1. Configure the Activity and associate the Activity with the navigation configuration file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <fragment
    	//设置为true表明会拦截返回键的操作
        app:defaultNavHost="true"
        //这里声明了用的是哪个导航配置文件
        app:navGraph="@navigation/my_nav"
        android:id="@+id/nav_host_fragment"
        //固定不变的
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </fragment>
</LinearLayout>

4. Jumping and passing values ​​between pages

//页面的跳转:两个方式都是一样的效果,用哪个都可以
//方式一
//这个view可以用onclick回调里的view
Navigation.findNavController(view).
//navigate里的id是导航配置文件action节点上的id
navigate(R.id.action_blankFragment_to_blankFragment2);

//方式二
//findNavController传入当前的fragment
NavHostFragment.findNavController(Fragment1.this).
//navigate参数同上
navigate(R.id.action_blankFragment_to_blankFragment2);


以上的方式是不传参的情况下,如果传参的话,navigate方法里是可以传Bundle的
第二个页面取值还是正常的fragment取值就行(getArguments())
public void navigate(@IdRes int resId, @Nullable Bundle args)
//两种方法都可以关闭当前页 返回上一页
Navigation.findNavController(v).navigateUp();
NavHostFragment.findNavController(Fragment3.this).navigateUp()

Guess you like

Origin blog.csdn.net/qq_36356379/article/details/109766401