Keep The Actionbar Displayed In When Changing Preferencescreen
Solution 1:
I finally managed to find a way to do this. It's kind of ugly but it works.
First I add an the same Intent to every PreferenceScreen definition in my preferences.xml file (make sure to update the value of the extra parameter)
<PreferenceScreenandroid:key="pref1"android:summary="Summary1"android:title="Title1" ><intentandroid:action="android.intent.action.VIEW"android:targetPackage="my.package"android:targetClass="my.package.activity.PreferencesActivity" ><extraandroid:name="page"android:value="pref1" /></intent>
...
</PreferenceScreen>
BTW my.package.activity.PreferencesActivity is my current Preference Activity
Then I add an intent-filter in the Manifest
<activityandroid:name=".activity.PreferencesActivity"android:configChanges="keyboardHidden|orientation|screenSize"android:label="@string/settings" ><intent-filterandroid:label="Pref" ><actionandroid:name="android.intent.action.VIEW" /><categoryandroid:name="android.intent.category.PREFERENCE" /></intent-filter></activity>
I add some code in the PreferenceActivity to handle this
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences_activity);
this.fragment = newPreferencesFragment();
this.fragment.setActivityIntent(getIntent());
getFragmentManager().beginTransaction()
.replace(R.id.container, this.fragment).commit();
}
Finally I add the following code in my PreferencesFragment class
publicvoidsetActivityIntent(final Intent activityIntent) {
if (activityIntent != null) {
if (Intent.ACTION_VIEW.equals(activityIntent.getAction())) {
if (intent.getExtras() != null) {
finalStringpage= intent.getExtras().getString("page");
if (!TextUtils.isEmpty(page)) {
openPreferenceScreen(page);
}
}
}
}
privatevoidopenPreferenceScreen(final String screenName) {
finalPreferencepref= findPreference(screenName);
if (pref instanceof PreferenceScreen) {
finalPreferenceScreenpreferenceScreen= (PreferenceScreen) pref;
((PreferencesActivity) getActivity()).setTitle(preferenceScreen.getTitle());
setPreferenceScreen((PreferenceScreen) pref);
}
}
Solution 2:
Had the same issue. Nested PreferenceScreen
s did not have an ActionBar. After stepping through the code, it appears to be caused by a conflict between AppCompatActivity
and PreferenceScreen
.
On one hand AppCompatActivity
provides its own action bar, and therefore requires a theme descending from Theme.AppCompat
which specifies windowNoTitle = true
somewhere (could not pinpoint exactly where). On the other -- PreferenceScreen
uses platform Dialog
with the activity theme (rather than sub-theme, e.g., dialogTheme
). Could be a bug.
If you don't care about Theme.AppCompat
, here's a simple workaround that works on API 21+:
- use
android.preference.PreferenceActivity
as the base class for your activity create a theme for that activity:
<!-- This theme is used to show ActionBar on sub-PreferenceScreens --><stylename="PreferenceTheme"parent=""><itemname="android:windowActionBar">true</item><itemname="android:windowNoTitle">false</item></style>
- specify
android:theme="@style/PreferenceTheme"
for this activity in theAndroidManifest.xml
What you'll get is more like a standard window title than a full ActionBar. I haven't yet figured out how to add a working back button, etc.
If you want to remain compatible with AppCompatActivity
and the related themes, you'll need to request FEATURE_NO_TITLE
on the activity window. Otherwise, you'll end up with two action bars (the built-in on top, and the support on bottom) in the top-level PreferenceScreen
.
Solution 3:
Since google sadly didn't fixed it until now, there is actually one much easier solution:
Set your SettingsActivity class to extend from just "Activity".
public class SettingsActivity extends Activity { ...
Create a new Theme in your v21/styles folder for your SettingsActivty and set the parent to "Theme.Material.*"
<style name="CustomThemeSettings" parent="android:Theme.Material"> <item name="android:colorPrimary">@color/...</item> <item name="android:colorPrimaryDark">@color/...</item> <item name="android:colorAccent">@color/...</item> </style>
Set your new theme in your Manifest.xml file:
<activity android:name=".SettingsActivity" android:label="@string/title_activity_settingsactivity" android:theme="@style/CustomThemeSettings" > </activity>
It just works :)
(Optional) If you want to provide Material Design support for older Devices you can put the SettingsActivity in an v21+ folder and create a other SettingsActivity for older devices which has the parent AppCompat.
Solution 4:
I have made an app that does have an action bar in the preferences activity. I can't seem to see the key to doing that, although I do remember it took me some time to nail it right.
It seems like our codes are quite similar. The only thing that gets to my attention is this import: import android.support.v7.app.ActionBarActivity;
Let me know if that helps any
publicclassSettingsActivityextendsActionBarActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, newPrefsFragment() ).commit();
} // End of onCreatestaticpublicclassPrefsFragmentextendsPreferenceFragment {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); // Load the preferences from an XML resource
}
} // end of PrefsFragment
}
Addition: do you have this in your styles.xml?
<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"></style>
Solution 5:
but as soon as I press on a PreferenceScreen preference, the actionbar is hidden. If I go back to the preference main screen I can see it again.
I guess you are launching a new activity when clicked on the preference
Your Preference fragment should look like this
publicstaticclassPreferencesFragmentextendsPreferenceFragment {
publicPlaceholderFragment() {
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.your_preferences);
}
}
Then sample your_preferences as below
<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"android:layout_width="fill_parent" ><PreferenceCategoryandroid:title="@string/pref_category_title"><PreferenceScreenandroid:title="@string/title"android:summary="@string/summary"><intentandroid:targetClass="com.your.package.Youractivity "android:targetPackage="com.your.package"/></PreferenceScreen><PreferenceScreenandroid:title="@string/another_title"><intentandroid:targetClass="com.your.package.activity2"android:targetPackage="com.your.package.activity"/></PreferenceScreen></PreferenceCategory>
And finally the main thing Youractivity should extend from ActionBarActivity
publicclassYouractivityextendsActionBarActivity {
}
The above code works for me.
Post a Comment for "Keep The Actionbar Displayed In When Changing Preferencescreen"