我有兩個活動 A 和 B 以及三個片段 C、D 和 E。
我使用意圖從活動 A 移動到 B 并且沒有完成活動 A,因為我想稍后再回到它。
Activity B 包含一個自定義布局和一個片段容器。
- 自定義布局包含一個文本視圖和一個后退圖示影像視圖。此布局充當操作欄,因為我不使用默認操作欄作為活動 B 的主題是 android:theme="@style/AppTheme.NoActionBar"。
- 片段容器托管一個導航圖。
片段 C 是起始目的地,導航圖允許導航為 C->D->E 并按以下順序回傳堆疊。
C->D //先回堆疊后
C //在第二個回傳堆疊到達回傳堆疊中的最后一個元素之后。
A //Activity B結束,再按一個后退圖示后Activity A恢復
在活動 B xml 中按下自定義布局的后退圖示影像視圖。
由于任何 Fragment(C, D, E) 中沒有其他操作欄,所以我只能使用活動 B 的后退圖示影像視圖。
我想知道如何實作這個功能 我的活動B 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--android:id="@ id/container"-->
<include layout="@layout/custom_tool_bar"
android:id="@ id/toolbar_settings_activity"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.fragment.app.FragmentContainerView
android:id="@ id/nav_host_dashboard_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/settings_navigation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar_settings_activity"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
下面給出了自定義工具列的 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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/app_gradient_color_background">
<androidx.appcompat.widget.AppCompatImageView
android:id="@ id/iv_back"
android:layout_width="20dp"
android:layout_height="26dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="1dp"
android:contentDescription="@string/content_description"
android:scaleType="fitXY"
android:src="@drawable/ic_white_color_back_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<LinearLayout
android:id="@ id/customlayout_width"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
app:layout_constraintStart_toEndOf="@ id/iv_back"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="@ id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingEnd="10dp"
android:paddingTop="6dp"
android:paddingStart="10dp"
android:text="@string/custom_tool_bar_Name"
android:textColor="@color/colorOffWhite"
android:textSize="26sp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
用作操作欄的自定義布局
導航圖開始目的地設定為 C 片段時的活動 B
Navgraph 在活動 B 中從片段 C->D->E 導航
uj5u.com熱心網友回復:
1-首先在您的活動 B 中獲取導航控制器:
val navHostFragment = binding.navHostDashboardFragment.getFragment<NavHostFragment>()
val navController = navHostFragment.navController
2-然后你可以在你的后退按鈕上添加一個點擊監聽器:
binding.toolbarSettingsActivity.ivBack.setOnClickListener {
val isStackPopped = navController.popBackStack() // Navigate back to the last fragment
if (!isStackPopped) finish() // If there is no fragments in the stack finish the activity and go back to activity A
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/513317.html
上一篇:警報對話框未全屏顯示
