Android官方推薦架構組件Navigation 讓單 Activity 應用成爲首選架構,更好的管理Fragment框架android
Navigation目前僅AndroidStudio 3.2以上版本支持,若是您的版本不足3.2, 下載AndroidStudio3.2以上版本。
官網下載地址:https://developer.android.google.cn/studio/git
快速開發, 組件可單獨使用,也能夠同時工做。 消除樣板 ,讓代碼Android 架構Jetpack管理乏味的活動事件,好比後臺任務、導航和生命週期管理。這樣你能夠專一於讓你的app更棒的東西,構建高質量、健壯的app 基於現代設計實踐,Android Jetpack組件能夠減小崩潰和內存泄漏,且向後兼容。接下來說述Navigation的使用以及如何管理多個Fragment等github
項目builde.gradle文件需配置:架構
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha05"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha05"app
1.建立MainActivity和佈局文件activity_navigation佈局裏配置: 框架
activity_navigation.xml佈局配置:ide
<fragment android:id="@+id/my_nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/mobile_navigation" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:labelVisibilityMode="labeled" app:menu="@menu/bottom_nav_menu" />
MainActivity裏配置:佈局
val host: NavHostFragment = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment? ?: return val navController = host.navController val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav_view) bottomNav?.setupWithNavController(navController)
二、新建HomeFragment、FlowStepFragment、FlowStepFragment、SettingsFragment、DeepLinkFragment在Fragment片斷裏跳片斷方法gradle
方法1: view.findViewById(R.id.navigate_destination_button).setOnClickListener( Navigation.createNavigateOnClickListener(R.id.next_action) )ui
方法2: view.findViewById(R.id.navigate_destination_button)?.setOnClickListener { findNavController().navigate(R.id.flow_step_one_dest, null, null) }
三、在res裏新建文件夾navigation 類型選擇Navigation,而後在這個文件夾裏建立mobile_navigation.xml
在mobile_navigation.xml裏寫入要跳轉的各個片斷Fragment及要傳遞的參數: startDestination默認第一個跳的片斷id
destination跳到另一個片斷id
action 隱式跳轉 ,argType傳遞參數類型,defaultValue傳遞參數值
<?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"
app:startDestination="@+id/home_dest">
<fragment android:id="@+id/home_dest" android:name="com.my.navigation.HomeFragment" android:label="@string/home" tools:layout="@layout/home_fragment"> <!--todo destination隱式跳轉到Fragement id=flow_step_one_dest --> <action android:id="@+id/next_action" app:destination="@+id/flow_step_one_dest" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> <fragment android:id="@+id/flow_step_one_dest" android:name="com.my.navigation.FlowStepFragment" tools:layout="@layout/flow_step_one_fragment"> <argument android:name="flowStepNumber" app:argType="integer" android:defaultValue="1"/> <action android:id="@+id/next_action" app:destination="@+id/flow_step_two_dest"> </action> </fragment> <fragment android:id="@+id/flow_step_two_dest" android:name="com.my.navigation.FlowStepFragment" tools:layout="@layout/flow_step_two_fragment"> <argument android:name="flowStepNumber" app:argType="integer" android:defaultValue="2"/> <action android:id="@+id/next_action" app:destination="@+id/settings_dest"> </action> </fragment> <fragment android:id="@+id/settings_dest" android:name="com.my.navigation.SettingsFragment" android:label="@string/settings" tools:layout="@layout/settings_fragment" > <action android:id="@+id/next_action" app:destination="@+id/deeplink_dest"> </action> </fragment> <fragment android:id="@+id/deeplink_dest" android:name="com.my.navigation.DeepLinkFragment" android:label="@string/deeplink" tools:layout="@layout/deeplink_fragment"> <argument android:name="myarg" android:defaultValue="Android!"/> </fragment>
</navigation>
<!--項目地址:https://github.com/Visen123/MyNavigation-->;