override fun onBackPressed() {
findNavController(R.id.fragmentContainerView2).popBackStack()
}
这是我用于后台导航的代码,但应用程序不会在主片段上退出,如果我在第一个片段上按下后退按钮,应用程序应该会关闭。如何做到这一点?
这是我的mainactivity.xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene2">
<ImageView
android:id="@+id/imageView7"
android:layout_width="60dp"
android:layout_height="50dp"
android:contentDescription="@string/logo"
android:src="@drawable/notification_icon"
app:layout_constraintBottom_toBottomOf="@+id/imageView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView8" />
<ImageView
android:id="@+id/image8"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:background="@color/white"
android:contentDescription="@string/navigation_drawer"
android:src="@drawable/three_lines"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thehelfen"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation2" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="match_parent"
app:cardBackgroundColor="@color/white"
app:cardElevation="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:clickable="true"
android:contentDescription="@string/logo"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/person"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/my_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/my_profile"
android:textColor="@color/black"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo" />
<ImageView
android:id="@+id/icon1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/consultnow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_profile" />
<TextView
android:id="@+id/consultnow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/consult_now"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon1"
app:layout_constraintStart_toEndOf="@+id/icon1"
app:layout_constraintTop_toTopOf="@+id/icon1" />
<ImageView
android:id="@+id/icon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/booklabtest"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon1" />
<TextView
android:id="@+id/booklabtest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/book_lab_test"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon2"
app:layout_constraintStart_toEndOf="@+id/icon2"
app:layout_constraintTop_toTopOf="@+id/icon2" />
<ImageView
android:id="@+id/icon3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/bookhealthpackage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon2" />
<TextView
android:id="@+id/bookhealth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/book_health_npackages"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon3"
app:layout_constraintStart_toEndOf="@+id/icon3"
app:layout_constraintTop_toTopOf="@+id/icon3" />
<ImageView
android:id="@+id/icon4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/info_icon"
app:layout_constraintBottom_toTopOf="@+id/contact_icon"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/about_us"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon4"
app:layout_constraintStart_toEndOf="@+id/icon4"
app:layout_constraintTop_toTopOf="@+id/icon4" />
<ImageView
android:id="@+id/contact_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/contact_us"
app:layout_constraintBottom_toTopOf="@+id/icon5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/contact_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/contact_us"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/contact_icon"
app:layout_constraintStart_toEndOf="@+id/contact_icon"
app:layout_constraintTop_toTopOf="@+id/contact_icon" />
<ImageView
android:id="@+id/icon5"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/faq"
app:layout_constraintBottom_toTopOf="@+id/icon6"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/faq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/faqs"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon5"
app:layout_constraintStart_toEndOf="@+id/icon5"
app:layout_constraintTop_toTopOf="@+id/icon5" />
<ImageView
android:id="@+id/icon6"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/tandc"
app:layout_constraintBottom_toTopOf="@+id/icon7"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/termsandconditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/terms_and_conditions"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon6"
app:layout_constraintStart_toEndOf="@+id/icon6"
app:layout_constraintTop_toTopOf="@+id/icon6" />
<ImageView
android:id="@+id/icon7"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/log_out"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/log_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/log_out"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon7"
app:layout_constraintStart_toEndOf="@+id/icon7"
app:layout_constraintTop_toTopOf="@+id/icon7" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.motion.widget.MotionLayout>
这是我的运动场景文件
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="500">
<KeyFrameSet>
<KeyAttribute
motion:motionTarget="@+id/cardView"
motion:framePosition="0"
android:translationX="-300dp"/>
<KeyAttribute
motion:motionTarget="@+id/cardView"
motion:framePosition="0"
android:translationX="0dp"/>
</KeyFrameSet>
<OnSwipe
motion:targetId="@+id/imageView8"
motion:autoCompleteMode="continuousVelocity"
motion:dragDirection="dragRight"
motion:maxAcceleration="20"
motion:maxVelocity="20"/>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
motion:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
motion:layout_constraintEnd_toStartOf="@+id/imageView7"
android:id="@+id/textView5"
motion:layout_constraintStart_toEndOf="@+id/image8" />
<Constraint
android:id="@+id/image8"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="15dp"
android:background="@color/white"
android:src="@drawable/three_lines"
motion:altSrc="@drawable/cancel_icon"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintHorizontal_bias="0.5"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:navGraph="@navigation/navigation2" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:visibility="invisible"
motion:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
motion:layout_constraintEnd_toStartOf="@+id/imageView7"
android:id="@+id/textView5"
motion:layout_constraintStart_toEndOf="@+id/image8" />
<Constraint
android:id="@+id/image8"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="300dp"
android:background="@color/white"
android:src="@drawable/three_lines"
motion:altSrc="@drawable/cancel_icon"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="250dp"
android:layout_height="750dp"
android:layout_marginTop="55dp"
android:layout_marginStart="400dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintHorizontal_bias="0.5"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:navGraph="@navigation/navigation2" />
</ConstraintSet>
</MotionScene>
这是我的主要活动.kt文件
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val contactus = findViewById<TextView>(R.id.contact_us)
val drawer = findViewById<ImageView>(R.id.image8)
val tandc = findViewById<TextView>(R.id.termsandconditions)
val motionLayout = findViewById<MotionLayout>(R.id.motion_layout)
contactus.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainerView2, contact_us_fragment())
commit()
motionLayout.transitionToStart()
}
}
drawer.setOnClickListener {
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}
tandc.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainerView2, TandCFragment())
commit()
motionLayout.transitionToStart()
}
}
}
}
是否可以设置不同的导航场景,如从about_us片段到home片段等
使用导航组件和NavHostFragment
时,永远不会执行FragmentTransaction
。相反,您应该按照指南导航到导航图中的目的地(您在NavHostFragment
.上设置的navigation/navigation2.xml
contactus.setOnClickListener {
// Use the Kotlin extension on Activity
// to get a reference to your NavController
val navController = findNavController(R.id.fragmentContainerView2)
// Use the android:id associated with your contact_us_fragment
// that needs to be in your navigation graph XML file
navController.navigate(R.id.contact_us_fragment)
motionLayout.transitionToStart()
}