How to implement dynamic "Up Navigation"?

6

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?

    
asked by anonymous 16.04.2014 / 21:46

3 answers

2

If you simply want to exit the Search Activity and return to the Activity that called you, after the user clicks the Up Navigation icon, complete the Search Activity.

this.finish();

I do not know if what I understand is what you want ...

    
16.04.2014 / 23:08
0

Is there a real need to use parentActivityName? Would not it be better to leave your city search activity without this feature? Since you have to go back to the activity that called your SearchCityActivity.

    
17.04.2014 / 19:46
0

I think you can do this in your onOptionsItemSelected () method and make a case switch for the id of the Home icon in ActionBar. I could have done this on a project of mine in the way I'm going to introduce you, but mindful of the remark that I used Activitys and Fragments, my navigation was mostly by Fragments. I did not declare the android: parentActivityName attribute or the no manifest.

I simply 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);
    }
}
    
10.05.2014 / 04:27