My app is Activity based and has several levels, i.e. ActivityA has actions to start ActivityB, which in turn starts ActivityC. Pressing 'back' from ActivityC returns to ActivtyB, and going back from that returns to ActivityA, as you would expect.
In my ActionBar I've enabled the Home icon, so that it displays the Up navigation arrow. I intended it to work in the same way as the Back button, but clicking Up from ActivityC still returns me to ActivityA, even though I've added the following to the manifest:
<activity android:name=".activity.ActivityC"
android:parentActivityName=".activity.ActivityB">
<!-- Parent activity meta-data to support API level 7+ -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.ActivityB" />
</activity>
Two questions:
1) Am I trying to misuse the ActionBar here? Should the Up icon always return you to the main activity?
2) Why is my implementation not working as I intended?
1) No, you are not trying to misuse the ActionBar here. If you are going from A -> B -> C then pressing Up from C should take you to B and NOT A. This is mentioned here as
"The Up button is used to navigate within an app based on the hierarchical relationships between screens. For instance, if screen A displays a list of items, and selecting an item leads to screen B (which presents that item in more detail), then screen B should offer an Up button that returns to screen A."
2)Your implementation is not working as intended because you forgot to do the following as mentioned here:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
I however do not recommend that you use the above technique to handle your Up Navigation as the above method will not work for ICS and below. The reason it won't work is because NavUtils behaves differently for Pre JellyBean and Post JellyBean as explained in this SO.
A better way is to do this is to handle the Up action manually in the Child Activity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(this, ParentActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP));
default:
return super.onOptionsItemSelected(item);
}
}
And best part of this manual approach is that it works for ALL Api Levels.