Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompatPreferenceActivity up button not working

Tags:

android

I'm trying to make an activity which extends AppCompatPreferenceActivity and implements an up button in the action bar. Visually everything looks fine, but the up button doesn't respond to touch events. The following is my java and xml code:

PrefrencesDisplayActivity.java

package com.example.matthew.testsettings;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.List;

public class PreferencesDisplayActivity extends AppCompatPreferenceActivity {
    CoordinatorLayout activityRoot;
    AppBarLayout appBarLayout;
    Toolbar toolbar;
    RelativeLayout contentRoot;
    ListView preferencesList;

    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preferences, target);
        initialiseUI();
    }

    private void initialiseUI() {
        // Inflate activity layout from xml
        setContentView(R.layout.activity_preferences_display);

        // Get handles to UI elements
        activityRoot = (CoordinatorLayout) findViewById(R.id.prefDisplay_activity_root);
        appBarLayout = (AppBarLayout) findViewById(R.id.prefdisplay_activity_appBarLayout);
        toolbar = (Toolbar) findViewById(R.id.prefDisplay_activity_toolbar);
        contentRoot = (RelativeLayout) findViewById(R.id.prefDisplay_content_root);
        preferencesList = (ListView) findViewById(android.R.id.list);

        // Create and configure action bar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

activity_prefrences_display.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:theme="@style/AppTheme.NoActionBar"
    android:id="@+id/prefDisplay_activity_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.matthew.testsettings.PreferencesDisplayActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/prefdisplay_activity_appBarLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/prefDisplay_activity_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_preferences_display"/>

</android.support.design.widget.CoordinatorLayout>

content_preferences_display.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/prefDisplay_content_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_preferences_display"
    tools:context="com.example.matthew.testsettings.PreferencesDisplayActivity">

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.example.matthew.testsettings"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".PreferencesDisplayActivity"
            android:label="@string/title_activity_preferences_display"
            android:parentActivityName="com.example.matthew.testsettings.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.matthew.testsettings.MainActivity"/>
        </activity>
    </application>
</manifest>

When I open the app and navigate to the preferences activity, the screen looks as it should but pressing the up button does nothing. Can anyone see my mistake?

like image 917
Helios Avatar asked Jan 07 '23 23:01

Helios


1 Answers

You can also just use finish() when the back button is pressed:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
like image 80
Prof Avatar answered Jan 27 '23 22:01

Prof