I have an app with a couple of activities. Until now I had a "hardcoded" hierarchy between them, so I had the android:parentActivityName set explicitly in each (but the launcher activity) of them. However now I added the Settings to the menu of more than one activity.
Is there a way to make the "back" arrow in the action bar function as the system's back button (in the bottom of the screen)? In other words: if there's a parentActivityName set, then it goes back to the parent activity, if not, then it closes the activity and "goes back" to the previous activity.
This is what I tried, but doesn't work. If I don't call actionBar.setDisplayHomeAsUpEnabled(true), then there's no "back" arrow in the action bar, but if I call it in an activity that doesn't have parentActivityName set in the Manifest, then I get an exception when I click the 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. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
final int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
}
}
Try :
The code above will call your
onBackPressed
, so be wary if you overrided it. Otherwise, it will be same like the "back" system button.