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
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?