AppCompatPreferenceActivity up button not working

I am trying to make an action that extends AppCompatPreferenceActivity and implements the up button in the action bar. Visually, everything looks fine, but the up button does not respond to touch events. Below is the 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 application and go to the settings activity, the screen looks as it should, but pressing the up button does nothing. Can anyone see my mistake?

+4
android
Dec 22 '15 at 7:26
source share
2 answers

You can also use finish() when you click the back button:

 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); } 
+8
Jun 11 '16 at 16:34
source share
 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); 

And override onBackpressed

 @Override public void onBackPressed() { super.onBackPressed(); } 
0
Dec 22 '15 at 7:31
source share



All Articles