我創建了一個簡單的演示專案來測驗 Android Jetpack Navigation 組件的默認回傳堆疊功能。
我有一個主要活動和兩個片段。應用程式運行時正在顯示主頁片段。主頁片段有一個按鈕。單擊導航到另一個片段。
但是每當我按下系統回傳按鈕時,我的完整應用程式就完成了,而不是通常的回傳堆疊行為,我應該在那里看到主片段。
任何幫助表示贊賞。提前致謝。
代碼如下:
MainActivity.kt
package com.callsamik.jetpacknavigationcomponentdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
activity_main.xml(主要活動的布局)
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@ id/nave_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.kt
package com.callsamik.jetpacknavigationcomponentdemo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.callsamik.jetpacknavigationcomponentdemo.databinding.FragmentHomeBinding
class HomeFragment: Fragment() {
var binding: FragmentHomeBinding? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(layoutInflater)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding?.loginButton?.setOnClickListener{
val action = HomeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
}
}
override fun onDestroyView() {
super.onDestroyView()
binding = null
}
}
fragment_home.xml(HomeFragment 的布局)
<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
android:gravity="center"
tools:context= ".HomeFragment">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Home Screen"
android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
LoginFragment.kt(第二個片段)
package com.callsamik.jetpacknavigationcomponentdemo
import androidx.fragment.app.Fragment
class LoginFragment: Fragment(R.layout.fragment_login) {
}
fragment_login.xml(第二個片段布局)
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp"
android:orientation="vertical"
tools:context=".LoginFragment">
<androidx.appcompat.widget.AppCompatEditText
android:id="@ id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"/>
<androidx.appcompat.widget.AppCompatEditText
android:id="@ id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/confirmButton"
android:text="Confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.appcompat.widget.LinearLayoutCompat>
nav_graph.xml(導航圖)
<?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/nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@ id/homeFragment"
android:name="com.callsamik.jetpacknavigationcomponentdemo.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@ id/action_homeFragment_to_loginFragment"
app:destination="@id/loginFragment"
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/loginFragment"
android:name="com.callsamik.jetpacknavigationcomponentdemo.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login" />
</navigation>
頂級 build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.31"
ext.nav_version = "2.3.5"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
應用模塊 build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.callsamik.jetpacknavigationcomponentdemo"
minSdk 22
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
viewBinding true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
uj5u.com熱心網友回復:
根據檔案:
- 該
app:defaultNavHost="true"屬性確保您NavHostFragment攔截系統的后退按鈕。請注意,只有一個NavHost可以是默認值。如果您在同一布局中有多個主機(例如,雙窗格布局),請確保僅指定一個 defaultNavHost。
您缺少該屬性,因此導航不處理系統后退按鈕。
<androidx.fragment.app.FragmentContainerView
android:id="@ id/nave_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357960.html
標籤:安卓 android-fragments 导航 后栈 喷气背包
