Help us understand the problem. What is going on with this article?

AndroidのnavigateUpToについて

More than 1 year has passed since last update.

ActivityのnavigateUpToとNavUtilsのnavigateUpToの違いが分からなくて調べたまとめです。

ActivityのnavigateUpTo

https://developer.android.com/reference/android/app/Activity.html#navigateUpTo

intentで指定されたアクティビティに移動し、アクティビティを閉じる
Android 4.1で追加された。

以下、Activity.javaにあるnavigateUpToのコード。

// Activity.java

    public boolean navigateUpTo(Intent upIntent) {
        if (mParent == null) {
            ComponentName destInfo = upIntent.getComponent();
            if (destInfo == null) {
                destInfo = upIntent.resolveActivity(getPackageManager());
                if (destInfo == null) {
                    return false;
                }
                upIntent = new Intent(upIntent);
                upIntent.setComponent(destInfo);
            }
            int resultCode;
            Intent resultData;
            synchronized (this) {
                resultCode = mResultCode;
                resultData = mResultData;
            }
            if (resultData != null) {
                resultData.prepareToLeaveProcess(this);
            }
            try {
                upIntent.prepareToLeaveProcess(this);
                return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
                        resultCode, resultData);
            } catch (RemoteException e) {
                return false;
            }
        } else {
            return mParent.navigateUpToFromChild(this, upIntent);
        }
    }

    public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
        return navigateUpTo(upIntent);
    }

NavUtilsのnavigateUpTo

https://developer.android.com/reference/android/support/v4/app/NavUtils.html#navigateUpTo

そもそも、NavUtilsとはAndroidのUIナビゲーションパターンを実装するためのクラス。
Support Packageに含まれているので、Android 1.6から使用することができる。
Android 4.1以降ではActivityのメソッドになっている。

以下、NavUtilsnavigateUpToのコード

// NavUtils.java

    private static final NavUtilsImpl IMPL;

    static {
        final int version = android.os.Build.VERSION.SDK_INT;
        if (version >= 16) {
            IMPL = new NavUtilsImplJB();
        } else {
            IMPL = new NavUtilsImplBase();
        }
    }

    public static void navigateUpTo(Activity sourceActivity, Intent upIntent) {
        IMPL.navigateUpTo(sourceActivity, upIntent);
    }

    static class NavUtilsImplBase implements NavUtilsImpl {
        @Override
        public void navigateUpTo(Activity activity, Intent upIntent) {
            upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            activity.startActivity(upIntent);
            activity.finish();
        }
    }

    static class NavUtilsImplJB extends NavUtilsImplBase {
        @Override
        public void navigateUpTo(Activity activity, Intent upIntent) {
            NavUtilsJB.navigateUpTo(activity, upIntent);
        }
    }
// NavUtilsJB.java

    public static void navigateUpTo(Activity activity, Intent upIntent) {
        activity.navigateUpTo(upIntent);
    }

まとめ

APIレベル16以降(Android4.1以降)ならNavUtilsnavigateUpToはactivityのnavigateUpToを使用しています。
それより前のAPIレベルはintentのfragにFLAG_ACTIVITY_CLEAR_TOPを追加してstartActivityを実行し、finisしているのがコードから分かります。

Android4.1以降のみをサポートするなら、ActivitynavigateUpToを使用で良いのかなと思います。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away