Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Up Navigation Doesn't Work

I'm trying to implement navigation according to: http://developer.android.com/training/implementing-navigation/ancestral.html

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                    // Navigate up to the closest parent
                    .startActivities();
        } else {
            // This activity is part of this app's task, so simply
            // navigate up to the logical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

but if (NavUtils.shouldUpRecreateTask(this, upIntent)) { is returning FALSE always.

How is this code useful? In which cases it does return true?

like image 207
Dandy Avatar asked Jul 01 '13 01:07

Dandy


1 Answers

On pre-Jelly Bean devices, per the source code of NavUtils, shouldUpRecreateTask is:

public boolean shouldUpRecreateTask(Activity activity, Intent targetIntent) {
    String action = activity.getIntent().getAction();
    return action != null && !action.equals(Intent.ACTION_MAIN);
}

Which uses the Activity's action (i.e., ACTION_VIEW, etc.) to determine if this Activity was launched from an external source. On Jelly Bean+ devices, it uses Activity.shouldUpRecreateTask (source code):

public boolean shouldUpRecreateTask(Intent targetIntent) {
    try {
        PackageManager pm = getPackageManager();
        ComponentName cn = targetIntent.getComponent();
        if (cn == null) {
            cn = targetIntent.resolveActivity(pm);
        }
        ActivityInfo info = pm.getActivityInfo(cn, 0);
        if (info.taskAffinity == null) {
            return false;
        }
        return !ActivityManagerNative.getDefault()
                .targetTaskAffinityMatchesActivity(mToken, info.taskAffinity);
    } catch (RemoteException e) {
        return false;
    } catch (NameNotFoundException e) {
        return false;
    }
}

Which uses Task Affinity to determine if the given Activity's affinity is the same as the affinity it was launched with (i.e., if Gmail launches your Activity, it would be given Gmail's affinity, rather than its own).

NavUtils.navigateUpTo, in all cases, launches an Activity. If it isn't launching the appropriate Activity, then you may need to look at what launch mode you are using and provide details on what platform version it isn't working.

like image 66
ianhanniballake Avatar answered Sep 20 '22 11:09

ianhanniballake