Show / return button on standby Android screen?

I have a two-level PreferenceScreen:

<PreferenceScreen> general settings <PreferenceScreen android:key="adv_settings"> more advanced settings </PreferenceScreen> </PreferenceScreen> 

My problem is that the back / up button on the action bar does not appear automatically on the second screen. How to make up button on adv_settings?

+12
java android android-actionbar
Jan 07 '13 at 6:02
source share
3 answers

You can add an arrow by writing your own ActionBar style that will be used with your application theme.

res / values-v11 / styles.xml: (or add them to existing .xml styles)

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="@android:style/Theme.Holo.Light"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:displayOptions">showHome|homeAsUp|showTitle</item> </style> </resources> 

Then apply this theme in your AndroidManifest.xml:

 <application android:theme="@style/MyTheme"> 


Note. The obvious way to add this arrow is to call:

 getActionBar().setDisplayHomeAsUpEnabled(true); 

after loading the second screen, but I think there is an Android error there, where getActionBar () always returns the first-level ActionBar object, and not the one that is currently visible, so the installation of the dynamic error fails.

+6
Apr 27 '13 at 0:37
source share

This may be more work, but you can create two PreferenceAtivity files, each with its own PreferenceFragment. Each PreferenceFragment will have its own XML PreferenceScreen (first level screen and second level screen). On the first level screen, you run the second PreferenceActivity function with the intent inside the tag. In the second PreferenceActivity, you can set the house icon by doing the following:

 ActionBar bar = getActionBar(); bar.setDisplayHomeAsUpEnabled(true); 

and then there was also a handler for the home button:

 @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } return false; } 

Assets:

 FirstPreferenceActivty FirstPreferenceFragment pref_first.xml (layout with PreferenceScreen and Prefernce nodes) SecondPreferenceActivty SecondPreferenceFragment pref_second.xml (layout with PreferenceScreen and Prefernce nodes) 
+2
May 08 '13 at 21:38
source share

If the button does not appear automatically, you can add it manually, as shown in these links:

Android: how to customize user preferences screen?

http://pastebin.com/334Eip35

In the second answer of this question, SO has sample code, and the snippet in it is probably taken from a different pastebin.

0
Jan 17 '13 at 14:40
source share



All Articles