ナビゲーションアイコンの表示

ナビゲーションアイコン

マーケットアプリなどActionBarのアプリアイコン横に表示されているこれ。

タッチすると前画面に戻ったり、別の画面に遷移させることができます。

ナビゲーションアイコン使用方法

ナビゲーションアイコンはActionBarの機能の一つです。
ActionBarクラスのsetDisplayHomeAsUpEnabled()メソッドをコールすることで使用可能になります。

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }


ナビゲーションアイコンをタッチするとonOptionsItemSelected()がコールされます。
その際、MenuItemのIdがandroid.R.id.homeとなります。

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked;
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

画面遷移だけではなく、好きな処理を割り当てることが可能ですね。