2015年10月13日 星期二

ActionBar 顯示返回鍵

Reference:

http://yuzhou2.blogspot.tw/2015/08/android-actionbar-homeasup.html
http://developer.android.com/guide/topics/ui/actionbar.html
https://developer.android.com/training/implementing-navigation/ancestral.html

Code snippet:

AndroidManifest.xml
<application ...>
    <activity android:name=".MainActivity" ...>
    </activity>
    <activity android:name=".SecondActivity"
              android:parentActivityName=".MainActivity" ...>
    </activity>
</application>


SecondActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    ActionBar actionBar = getSupportActionBar();
    // change the icon of key
    actionBar.setHomeAsUpIndicator(R.drawable.icon);
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Note:

This is appropriate when the parent activity may be different depending on how the user arrived at the current screen.

SecondActivity.java
@Override
public Intent getParentActivityIntent() {
    finish();
    return null;
}

沒有留言: