Search code examples
androidnavigationandroid-actionbarpreferences

Dynamic parent activity for Android up \ back navigation in Preferences menu


I've a Preference activity for a options menu in my Android app. I've enabled the Up \ Back navigation on the ActionBar and I need to come back to the previous activity that called the Options menu. For the Preference activity, I could use in the manifest:

android:parentActivityName="mypackage.com.MainActivity"

but how come back to other activies ? The Options menu is called from 4 different activities.

public class Prefs extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);


    ActionBar actionBar = getActionBar();
    // Enabling Up / Back navigation
    actionBar.setDisplayHomeAsUpEnabled(true);


}

}


Solution

  • Solved in this way:

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                  finish();
    
            }
            return true;
    
        }