I'm trying to implement Up Navigation in my Android app but apparently, I did not find any way to make android:parentActivityName
, set in the manifest , be manipulated at runtime.
What happens in my case is that, for example, I have a Cities Search Activity, which can be called from various Activity's, being that by clicking on the "UP Navigation" icon, it would be necessary to go back to the This activity should be dynamic, not preconfigured in the manifest
Is there any way to make this dynamic behavior , for example, by searching for the last activity in the stack?
What I have implemented so far is this:
In manifest :
<activity
android:name="com.myapp.SearchCityActivity"
android:parentActivityName="com.myapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapp.MainActivity" />
</activity>
No% of search activity:
getActionBar().setDisplayHomeAsUpEnabled(true);
And I'm overwriting the android:parentActivityName
method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
Is there anything else I can do to get to the expected behavior?