android - Is there a way to make AppCompat's ActionBar's "back/up" button act as the "back" system button -


i have app couple of activities. until had "hardcoded" hierarchy between them, had android:parentactivityname set explicitly in each (but launcher activity) of them. added settings menu of more 1 activity.

is there way make "back" arrow in action bar function system's button (in bottom of screen)? in other words: if there's parentactivityname set, goes parent activity, if not, closes activity , "goes back" previous activity.

this tried, doesn't work. if don't call actionbar.setdisplayhomeasupenabled(true), there's no "back" arrow in action bar, if call in activity doesn't have parentactivityname set in manifest, exception when click arrow.

abstract public class baseactivity extends actionbaractivity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(getlayoutresourceid());          final actionbar actionbar = getsupportactionbar();         if (null != actionbar) {             actionbar.setdisplayhomeasupenabled(getdisplayhomeasupenabled());         }     }      protected abstract int getlayoutresourceid();     abstract protected boolean getdisplayhomeasupenabled();      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         final int id = item.getitemid();         if (id == android.r.id.home) {             navutils.navigateupfromsametask(this);             return true;         }     } } 

try :

    if (id == android.r.id.home) {         onbackpressed();         return true;     } 

the code above call onbackpressed, wary if overrided it. otherwise, same "back" system button.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -