6
I’m trying to implement Up Navigation on my Android app but apparently I found no way to do that android:parentActivityName
, set in the manifest, can be manipulated at runtime.
What happens in my case is that, for example, I have a City Search Activity, which can be called from several Activity’s, and when clicking on the "UP Navigation" icon, it would be necessary to return to the previous Activity (which called the Research Activity)not for the Activity set in android:parentActivityName
, this behavior should be dynamic, and not pre-configured in manifest
Is there any way to make this behavior dynamic, for example, searching for the last Activity of the stack?
What I’ve implemented so far is this:
In the 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>
In the onCreate()
of Research Activity:
getActionBar().setDisplayHomeAsUpEnabled(true);
And I’m overwriting the method onOptionsItemSelected
:
@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);
}
There’s something else I can do to get to the expected behavior?
That’s not quite what I wanted, but this simple approach works for my case, I hadn’t thought about it in a simplified way. Thank you.
– Fernando Leal