0

I just created new Android Bottom Navigation application by Android Studio template and add a button to do as bellow: Button click => set button visibility = gone

And I having a problem that after click on 2nd tab then back to first tab, the button is visible again. How can I keep button state as gone when back from 2nd tab

enter image description here

Here is my step to create example project New Project => Bottom Navigation View Activity

and add bellow code to HomeFragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding.imvTest.setOnClickListener {
        binding.imvTest.visibility = View.GONE
    }
}

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=".ui.home.HomeFragment">

    <EditText
        android:id="@+id/text_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:importantForAutofill="no"
        android:textAlignment="center"
        android:textSize="20sp"
        android:hint="Type something"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:contentDescription="@null"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_home" />
</androidx.constraintlayout.widget.ConstraintLayout>

For simple UI as my example, I can save image's state = visible or gone, but how about large and complication UI? How can I save view's state

0 Answers0