проблема с кнопкой назад в тулбаре
Есть 3 фрагмента они переключаются через шторку которая выдвигается слева или тремя полосками сверху слева(в тулбаре) с них можно перейти на 4 фрагмент на нем место трех полосной кнопки появляется стрелка и она превращается в кнопку назад и мне надо ее заставить помимо ее работы еще и сохранять данные как мне это сделать из фрагмента пытался так
var toolb = view.findViewById<Toolbar>(R.id.toolbar)
toolb.setNavigationOnClickListener {
back()
}
но это вызвало ошибку
Attempt to invoke virtual method 'void android.widget.Toolbar.setHasTransientState(boolean)' on a null object reference
код экрана
<?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:id="@+id/constraint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="0dp"
android:layout_height="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.001">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.appbar.AppBarLayout>
<include
layout="@layout/content_main"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
код фрагмента
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/editlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.MyApplication.FullscreenContainer"
tools:context=".Edit">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="66dp">
<EditText
android:id="@+id/nameplaintext"
android:layout_width="373dp"
android:layout_height="match_parent"
android:ems="10"
android:hint="Заголовок"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="43dp"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:background="@android:color/transparent"
android:text="Button"
app:icon="@drawable/free_icon_microphone_3364184"
app:iconGravity="top"
app:iconSize="43dp"
app:iconTint="#000000"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
</LinearLayout>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlistlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="invisible">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/button3"
android:layout_width="113dp"
android:layout_height="140dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<MultiAutoCompleteTextView
android:id="@+id/noteplaintext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:filterTouchesWhenObscured="false"
android:fitsSystemWindows="false"
android:gravity="start"
android:hint="Заметка"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
Источник: Stack Overflow на русском