Android向上导航不起作用

我试图按照以下链接实现导航: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);
}

if (NavUtils.shouldUpRecreateTask(this, upIntent)) {始终返回FALSE。

这段代码如何有用? 在哪种情况下,它确实返回true?


在预Jelly Bean设备上,根据NavUtils的源代码, shouldUpRecreateTask是:

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

其中使用了Activity的操作(即ACTION_VIEW等)来确定此Activity是否从外部源启动。 在Jelly Bean +设备上,它使用Activity.shouldUpRecreateTask (源代码):

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;
    }
}

它使用Task Affinity来确定给定Activity的亲和力是否与其启动的亲和力相同(即,如果Gmail启动了您的Activity ,则会给予Gmail的亲和力,而不是它自己的亲和力)。

NavUtils.navigateUpTo在任何情况下都会启动一个活动。 如果它没有启动相应的Activity ,那么您可能需要查看您正在使用的启动模式,并提供有关哪个平台版本无法运行的详细信息。


我通过使用ActivityManager并检查backStack条目来解决它。

我希望这对大家都有用。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:

            ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
            List<RunningTaskInfo> runningTaskInfoList =  am.getRunningTasks(10);
            List<String> backStack = new ArrayList<String>();
            Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
            while(itr.hasNext()){
                RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
                String topActivity = runningTaskInfo.topActivity.getShortClassName();
                backStack.add(topActivity.trim());
            }
            if(backStack!=null){
                if(backStack.get(1).equals(".MainActivity")){
                    moveTaskToBack(true); // or finish() if you want to finish it. I don't.
                } else {
                    Intent intent = new Intent(this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }
            }

        break;  
    }
}

我需要使用:

<uses-permission android:name="android.permission.GET_TASKS" />

在我的Manifest中。

它完美的工作,虽然它不是我想要的解决方案。 不幸的是,android仍然缺乏先进的内置方法来实现简单的事情,我们都必须找到一切的方式。

链接地址: http://www.djcxy.com/p/72469.html

上一篇: Android Up Navigation Doesn't Work

下一篇: How to securely use window.postMessage when the sender domain is unknown