How do I close the PreferenceScreen?

Asked 2 years ago, Updated 2 years ago, 110 views

I created an application with user settings using Kotlin and the Preference Library, but I don't know how to close the settings screen.
I want the title bar to go back when I press that button on top.
(I don't want to display the title bar except on the setting screen because it looks ugly.)

sample
Screenshot

setting.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="OnOff"
        android:summaryOn="On"
        android:summaryOff="Off"
        android: title="OnOff"/>

    <ListPreference
        android:defaultValue="OFF"
        android:entries="@array/reply_entries"
        android:entryValues="@array/reply_values"
        android:key="level"
        android: title="List preference"
        app:useSimpleSummaryProvider="true"/>

</PreferenceScreen>

Seeting.kt

package com.example.myapp

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.preference.PreferenceFragmentCompat
import com.example.sugoidentaku.R

class Setting:PreferenceFragmentCompat(){
    override fun onCreatePreferences (savedInstanceState: Bundle?, rootKey: String?) {
        // Configure the settings screen according to the res/xml/preferences.xml file
        setPreferencesFromResource(R.xml.setting, rootKey)
    }

MainActivity.kt

supportFragmentManager
            .beginTransaction()
            .replace(R.id.setting,Setting())
            .commit() 

android kotlin xml android-fragments

2022-09-30 18:11

1 Answers

Wouldn't it be better to save the transaction with addToBackStack?

supportFragmentManager
            .beginTransaction()
            .replace(R.id.setting,Setting())
            .addToBackStack (null)
            .commit()


2022-09-30 18:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.