How to implement dynamic "Up Navigation"?

Asked

Viewed 460 times

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?

3 answers

2


If you just want to quit Search Activity and go back to the Activity that called you, after the user clicks the Up Navigation icon and finishes the Search Activity.

this.finish();

I don’t know if what I understand is what you want...

  • 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.

0

I think you can do this in your onOptionsItemSelected() method and switch to the Home icon id in Actionbar. I could have done this in a project of mine the way I’m going to present you, but attentive to the observation that I used Activitys and Fragments, my navigation was almost all for Fragments. I did not declare the android attribute:parentActivityName nor the in the manifest.

I just implemented the menu case like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.home:
            // é aqui que você volta para a activity ou fragment anterior
            Intent parentActivityIntent = new Intent(this, SuaActivityAnterior.getClass() );
            // Remove todas as outras activitys que estão na pilha
            parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(parentActivityIntent);
            finish();
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
}
  • I am using Activity’s

  • @Fernando edited my post with maybe a possible solution and how I would implement.

0

There is real need for parentActivityName use?
Wouldn’t it be better to leave your search for cities without this resource? Since you have to go back to the Activity that called your Searchcityactivity.

  • is necessary to maintain the pattern of navigation within the application, as I am implementing this type of navigation in all Activity’s, I am only having problems in these cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.