Move back from settings

I play Android Studio, so I created the SettingsActivity application using the wizard, and I ran into a problem that made it impossible to switch from this settings activity to the main activity using the up arrow in the action bar.

The action bar setup looks like this:

private void setupActionBar() { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // Show the Up button in the action bar. actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); } } 

Actionbar is not null btw.
And parentActitvityName is set in AndroidManifest:

  <activity android:name=".SettingsActivity" android:label="@string/title_activity_settings" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.demo.app.MainActivity" /> </activity> 

However, clicking on the arrow does nothing. Even onOptionsItemSelected does not work.

This seems to be exactly the same problem. The setDisplayHomeAsUpEnabled action bar does not work on ICS , but switching from part to overview activity works fine in the same application. In addition, I set MinSDK to 15 and TargetSDK to 23.

+6
source share
2 answers

override the onOptionsItemSelected method in your activity and do it as follows

  @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar Up/Home button case android.R.id.home: super.onBackPressed(); return true; } return super.onOptionsItemSelected(item); } 
+16
source

Vspallas answer is correct. The error was on my side. I had an onOptionsItemSelected method inside a preferenceFragment, not in an Activity. Mea culpa.

+2
source

All Articles