current position:Home>Talk about "single activity + multiple fragments" mode

Talk about "single activity + multiple fragments" mode

2022-01-27 03:50:48 xlu1997

This should be MVVM The third article on the development framework , The first two articles can be viewed :
Write a MVVM Rapid development framework ( One ) Basic class encapsulation
Write a MVVM Rapid development framework ( Two ) Component transformation

single Activity+ many Fragment Pattern

Since I know this move, I'm basically reluctant to use activity 了 ,fragment You can quickly create and manage , Can reasonably design page Jump , Design Cool jump animation , Some operations can be managed uniformly .

  1. use Fragment replace Activity

In the past, most of the time will be Activity As a page ,Fragment As a sub page in the page ( It was called debris ), Basically, most functions consist of activity Realization , For example, the old version of Taobao app There are hundreds activity, Caton didn't want it at that time . With technology iterations , We found that activtiy establish 、 Switch 、 Destruction consumes far more performance than fragment Be big ,fragment Now it can also replace activity Implement most of the functions .

  1. take Activity As a container

I understand. single Activity+ many Fragment A pattern does not mean a App There must be only one activity, For some business-related scenarios , Can be integrated into one single Activity+ many Fragment modular , take activity As fragment The container of , Give Way fragment To do UI Drawing work .

  1. management Fragment Stack

We can use navigation management fragment,fragment Jump between 、 Stack management is easy ,navigation You can also animate the switch 、 Data transfer between pages .

Navigation Components

Navigation yes Jetpack One of the components , A long time ago iOS That's the way you jump , I was thinking Android Why not , It didn't take long Navigation It's coming out .

Navigation It can be understood as a management fragment The container of , In the container fragment Can achieve any jump ,

Based on using :

  1. We need to create... In the layout Fragment Containers :
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_main"/>
 Copy code 
  1. establish navigation.xml file
<?xml version="1.0" encoding="utf-8"?>
<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/navigation_main"
    app:startDestination="@+id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.mvvm_develop.MainFragment"
        android:label="MainFragment" />


</navigation>
 Copy code 
  1. Use NavController
val navController = (childFragmentManager.findFragmentById(R.id.module_fragment_container) as NavHostFragment).navController
// Jump 
navController.navigate(R.id.mainFragment)
 Copy code 

Some specific parameters and usage :

navGraph

This value points to xml file , stay xml In the document we can define fragment, Jump behavior , Destination, etc .

establish 、 newly added Fragment:

Create jump behavior :

Animation 、 Destination 、 Returns the stack configuration :

NavController

It literally means navigation controller ,NavController You can control the jump 、 return 、 Animation 、 Monitor and other operations . We can use it for flexible jumps ,Google There are also some Navigation Demo Demonstrate how to cooperate with Toolbar And the bottom navigation bar .

I won't explain the specific usage here , There are many articles , You can also refer to Official website .

Navigation The problem is :

Go back to the life cycle

Navigation At present, there is a problem :Fragment Go back and walk the life cycle , The question may be Google Want to make Fragment and activity Have the same working mode , It's really annoying to walk a single life cycle , We can customize it NavHostFragment To fix this problem , Specific reference Project code

After modification, use the following :

    android:name="androidx.navigation.fragment.NavHostFragment"
     Modify it to our custom NavHostFragment:
    android:name="com.example.baselibrary.navigation.NavHostFragment"
    
    <fragment
        android:id="@+id/navigation_main"
        android:name="com.example.baselibrary.navigation.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_main"/>
 Copy code 

Used in componentization Navigation

We usually use the bottom navigation bar to app Divide different functions , These are all separate module, But in navigation How to proceed in module The jump between ?

such as :

Its layout file is a FragmentContainerView+BottomNavigationView, When switching the following buttons, you need to switch to different moduel page . First, we'll look at different moduel Think of it as a “ single activity+ many fragment” Module , Or you can omit activity.

Mode one :
google Of demo It's in MainActivity Create a main_navGraph, It contains different sub moduel Of navGraph , as follows :

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_module"
    app:startDestination="@+id/navi_home">

    <include app:graph="@navigation/navi_home"/>
    <include app:graph="@navigation/navi_collection"/>
    <include app:graph="@navigation/navi_center"/>

</navigation>
 Copy code 

navi_homenavi_collectionnavi_center Son moduel Medium navGraph file , This practice requires it to specify startDestination, And can only jump to startDestination

coordination BottomNavigationView Use :

val navController = (childFragmentManager.findFragmentById(R.id.module_fragment_container) as NavHostFragment).navController
setupWithNavController(binding.bottomNav,navController)
 Copy code 

In this way, we can really achieve moduel Switch between , But I found that this method switches every time naviagtion Will be reinitialized , This leads to high performance consumption .

Maybe I use the wrong posture ?

Mode two :
because app module Itself is dependent on each child moduel Of , We can do it in navGraph Direct use of sub moduel Medium Fragment, The main page only needs to add each module The Lord of Fragment That's it :

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_module"
    app:startDestination="@+id/navi_home">
    <fragment
        android:id="@+id/navi_home"
        android:name="com.xlu.module_tab1.HomeFragment"
        android:label="HomeFragment" />
    <fragment
        android:id="@+id/navi_collection"
        android:name="com.xlu.module_collection.FragmentCollection"
        android:label="CenterFragment" />
    <fragment
        android:id="@+id/navi_center"
        android:name="com.xlu.module_center.CenterFragment"
        android:label="FragmentCollection" />

</navigation>
 Copy code 

Switch directly when switching the bottom state Fragment That's it :

        val navController = (childFragmentManager.findFragmentById(R.id.module_fragment_container) as NavHostFragment).navController

        binding.bottomNav.setOnItemSelectedListener(object :NavigationBarView.OnItemSelectedListener{
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                navController.navigate(item.itemId)
                return true
            }
        })
 Copy code 

If you are lazy, you can BottomNavigationView The use of menu Medium id And navGraph Set it to the same ah ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha


Basically ARouter+Navigation It can meet most of the page Jump needs , But there are still some difficulties , It's just different moduel Between navGraph How to control each other , I haven't thought of a good solution for the time being ( It can be implemented by providing interface services through the above-mentioned alternatives ), After all Navigation I'm not going to prepare for componentization .

The last attached mvvm_develop Project address , Humble Androider Online search star

copyright notice
author[xlu1997],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270350428969.html

Random recommended