I have preference screen extended PreferenceActivity
. For targeting OS 4.0.3, I wanted to add <
icon on action bar so I did this in onCreate()
.
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
It worked. <
was added to left of app icon. But when I tap the item which goes into the next level (more detail screen), the <
won't be displayed. Returning to the top level, the <
appears again.
I have never thought about a mechanism of nested preference since smart the PreferenceActivity
hides it. Now my question is, why won't PreferenceActivity
display the <
on nested preference?
I don't want to argue that I don't need to add <
to the preference screen. (Even some of Google's app add, some don't, so I think there is no solid rule for this.)
If there is a simple solution for this, I want to solve this issue.
Instead of dynamically adding this, you should add the arrow by writing a custom ActionBar style to be used with your application theme. (Basically, see https://stackoverflow.com/a/16247111/582004)
This is my working code for Home button on Settings activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
root.addView(bar, 0); // insert at top
bar.setTitle("Settings");
bar.setTitleTextColor(Color.WHITE);
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
addPreferencesFromResource(R.layout.settings);
}
This is the settings_toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="2dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/gobackleftarrow"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
You need to enable the home button and actionbar for each Activity that you wish to show the UP navigation. You must also set the parent activity in the manifest.
See this question for the gory details: Up indicator does not show up
来源:https://stackoverflow.com/questions/11971800/sethomebuttonenabled-on-preferenceactivity-and-nested-preference