Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android studio – Back button not working on Preferences multiple screens

Posted on November 14, 2022

Solution 1 :

You need to override the default behavior of android.R.id.home to onBackPressed.

class SettingsActivity : AppCompatActivity() {

    ...

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                onBackPressed()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    ...

Solution 2 :

Found a good complete example here https://github.com/googlearchive/android-preferences

Problem :

I am trying to implement preferences using multiple screens. The action bar back button only returns to MainActivity even for sub preference screens. For example, I have a root preference screen and then a sub reference screen. The back button on the root preference screen goes back to the main activity as expected. But the back button on the sub reference screen also goes back to the main activity rather than the root preference screen. Below is a code dump because I’m not sure what I am doing wrong. Thanks.

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/apk/res/android"
    package="com.example.PreferencesTest">

    <application
        android_allowBackup="true"
        android_icon="@mipmap/ic_launcher"
        android_label="@string/app_name"
        android_roundIcon="@mipmap/ic_launcher_round"
        android_supportsRtl="true"
        android_theme="@style/AppTheme">
        <activity
            android_name=".SettingsActivity2"
            android_label="@string/title_activity_settings2"
            android_parentActivityName=".SettingsActivity">
            <meta-data
                android_name="android.support.PARENT_ACTIVITY"
                android_value=".SettingsActivity" />
        </activity>
        <activity
            android_name=".SettingsActivity"
            android_label="@string/title_activity_settings"
            android_parentActivityName=".MainActivity">
            <meta-data
                android_name="android.support.PARENT_ACTIVITY"
                android_value=".MainActivity" />
        </activity>
        <activity
            android_name=".MainActivity"
            android_theme="@style/AppTheme.Launcher">
            <intent-filter>
                <action android_name="android.intent.action.MAIN" />
                <category android_name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
MainActivity.kt

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    //Inflates settings menu button
    override fun onCreateOptionsMenu(menu: Menu): Boolean
    {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.settings_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean
    {
        return when(item.itemId)
        {
            R.id.action_settings ->
            {
                val intent = Intent(this, SettingsActivity::class.java)
                startActivity(intent)
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}
SettingsActivity.kt

class SettingsActivity : AppCompatActivity()
{

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)

        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings, SettingsFragment())
            .commit()

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

    }

    class SettingsFragment : PreferenceFragmentCompat()
    {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
        {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)
        }
    }
}
SettingsActivity2.kt

class SettingsActivity2 : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings, SettingsFragment())
            .commit()
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.preferences2, rootKey)
        }
    }
}
res > menu > settings_menu.xml

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

    <item
        android_id="@+id/action_settings"
        android_orderInCategory="100"
        android_title="Settings"
        app_showAsAction="ifRoom" />

</menu>
res > xml > root_preferences.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">
    <Preference
        android_fragment="com.example.PreferencesTest.SettingsActivity2$SettingsFragment"
        android_title="Title"
        android_summary="Summary">
        <extra
            android_name="name"
            android_value="preferences2" />
    </Preference>
</PreferenceScreen>

res > xml > preferences2.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">
    <EditTextPreference
        app_key="callNumber"
        app_title="Call Number"
        android_inputType="phone"
        app_useSimpleSummaryProvider="true" />
</PreferenceScreen>

Comments

Comment posted by daniel.jbatiz

Do override the OnSupportNavigateUp method in your activity

READ  [ANSWERED] sqlite - Why there is no android database inspector tools in android studio 4.2 canary 13?
Powered by Inline Related Posts

Comment posted by Tom

@daniel.jbatiz, doesn’t work. I noticed two things. First, I don’t think onSupportnavigateUp is called if parent activity is set in the manifest. Second, the sub preference displays the root preference title rather than it’s own. Its as if the sub activity is using the root action bar. Because of this, I’m not convinced I have implemented the sub preference correctly. Do you know of a basic tutorial using sub preferences with a back button?

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress