Search code examples
androidandroid-studiokotlinandroid-fragmentsandroid-preferences

Android Studio Preferences issue with creating settings


I'm currently trying to implement a settings function on android studio using kotlin. I am referencing this guide. I am using a mac and an external android device to run the applications

However, I have run into an issue where my application runs successfully (opens, other aspects of application performs fine), but crashes whenever i try to click on the "settings" option.

Below are functions from my MainActivity class

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.iSettings) {
            val intent = Intent(this, SettingsActivity::class.java)
            startActivity(intent)
        }
        return super.onOptionsItemSelected(item)
    }

Below is the code for my SettingsActivity class

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        supportFragmentManager
            .beginTransaction()
            .replace(android.R.id.content, MySettingsFragment())
            .commit()
        super.onCreate(savedInstanceState)
    }
}

Below is the code for MySettingsFragment class

class MySettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)
    }
}

My xml code for the settings/preferences page. Currently filled with code examples from the guide above

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

    <SwitchPreferenceCompat
        android:key="notifications"
        android:title="Enable message notifications"/>

    <Preference
        android:key="feedback"
        android:title="Send feedback"
        android:summary="Report technical issues or suggest new features"/>

</androidx.preference.PreferenceScreen>

Also, my menu xml code

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

    <item
        android:id="@+id/iSettings"
        android:orderInCategory="100"
        android:title="Settings"
        app:showAsAction="never"/>
</menu>

Dropdown menu at top right corner

Settings option which crashes the application

Please help!


Solution

  • Looks good, but we need a little bit more context on the problem, like how are your xml set. I'll write some notes so you can determine what's missing on your app.

    1. Preferences files should be in res/xml/preferences.xml
    2. In your SettingsActivity you should have a FrameLayout with an id that you could set in your .replace(android.R.id.content, MySettingsFragment()) and instead of android.R.id.content you use yours.
    3. onCreateOptionsMenu should return a Boolean and you are always sending the same return super(..) you should change it to :
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when(item.itemId){
            R.id.iSettings -> {
                val intent = Intent(this, SettingsActivity::class.java)
                startActivity(intent)
                true
            }
            else -> return super.onOptionsItemSelected(item)
        }
    }
    
    1. Have you declared your Activity in the manifest.xml?
    2. You should replace the fragment after the setContentView in your SettingsActivity as :
    class SettingsActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            supportFragmentManager
                    .beginTransaction()
                    .replace(R.id.your_id, MySettingsFragment())
                    .commit()
        }
    }