Android: How to go from the main preferences screen through the action bar in xml?

I have a modern SettingsActivity using PreferenceFragement . I have fully defined its layout in XML. In my setup, I have a sub-screen preference. By clicking on it, a new PreferenceScreen will appear.

It seems to me that I can return to the main settings screen using the "Back" button. Is there a way to enable navigation through an ActionBar in a sub-screen? I would prefer to include it via XML.

Thanks in advance, kaolick

EDIT:

I want the same thing on the right side as on the left side:

enter image description here

The following is a snippet of code from Google:

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <!-- opens a subscreen of settings --> <PreferenceScreen android:key="button_voicemail_category_key" android:title="@string/voicemail" android:persistent="false"> <ListPreference android:key="button_voicemail_provider_key" android:title="@string/voicemail_provider" ... /> <!-- opens another nested subscreen --> <PreferenceScreen android:key="button_voicemail_setting_key" android:title="@string/voicemail_settings" android:persistent="false"> ... </PreferenceScreen> <RingtonePreference android:key="button_voicemail_ringtone_key" android:title="@string/voicemail_ringtone_title" android:ringtoneType="notification" ... /> ... </PreferenceScreen> ... </PreferenceScreen> 
+6
source share
1 answer

First add getActionBar().setDisplayHomeAsUpEnabled(true); in onCreate() .

Then try:

 @Override public boolean onOptionsItemSelected(MenuItem item) { if(android.R.id.home == item.getItemId() ){ // try one of these: // dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); // getFragmentManager().popBackStack(); // finish(); return true; } return super.onOptionsItemSelected(item); } 
+1
source

Source: https://habr.com/ru/post/949407/


All Articles