简体   繁体   中英

Adding ActionBar with the arrow “GoBack” to PreferenceActivity?

I have a PreferenceActivity. I'm looking for a way to add ActionBar with the arrow "GoBack" to it. All the examples I found so far have been, to my mind, overcomplicated because if I had a simple Activity I could add ActionBar to it with one line of java code and that would be it.

I wonder, isn't there a simple way to add ActionBar with the arrow "GoBack" to PreferenceActivity?

UPDATE:

Here's my Preference activity:

public class PreferenceActivity123 extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
    }

    public static class MainPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
}
// Add this in your androidmanifest.xml file
     <activity
                android:name=".SecondActivity"
                android:label="@string/app_name"
                android:parentActivityName=".MainActivity">

                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value=".MainActivity" />
            </activity>

You have to set DisplayHomeasUpEnabled in your actionbar activity like

getSupportActionBar().setDisplayHomeAsUpEnabled(true);//Which will show back button

Define the parent activity in AndroidManifest.xml where the activity(PreferenceActivity) will be called once the back button in the action bar is pressed.

In your definition on the Manifest, add the line:

 <activity
    android:parentActivityName="com.example.activities.PreferenceActivity"
  </activity>

or Just listen the optionItemSelected method

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, PreferenceActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Edit : In order to achieve same in Preference activity You need to make custom action bar style with back button in styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>    
  <style name="PrefTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/PrefActionBar</item>
  </style>

  <style name="PrefActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
  </style>
</resources>

Call the style in manifest like :

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

Call the action bar in activity

getActionBar().setDisplayHomeAsUpEnabled(true);

Just use android.support.v7.widget.Toolbar

    public class SettingsActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);       

 Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar);
        actionbar.setTitle("Settings");
        actionbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
        actionbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YourActivity.this.finish();
            }
        });

        }
    }

and your activity_settings.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              tools:context=".SettingsActivity"
              tools:menu="settings"
              tools:actionBarNavMode="standard"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <android.support.v7.widget.Toolbar
    android:id="@+id/actionbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark"
    />

    <FrameLayout 
        android:id="@id/content"
        android:orientation="vertical"    
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM