繼上次 [Android] ActionBar Menu/MenuItem in Different Fragments,這次要來在不同的 fragment 設定 AcitonBar title 啦~
因為我使用了 ViewPager,其實他會預讀左右兩邊的 fragment, 導致你的 onResume() 其實會被提前呼叫,也就是如果在 onResume() 裡更改 ActionBar title 的話會有重複蓋過的情形發生,那要怎麼確保 現在這個 fragment 正在眼前 呢?
請看:How to determine when Fragment becomes visible in ViewPagerSetting Custom ActionBar Title from Fragment

In your Activity:

public void setActionBarTitle(String title){
    YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);
}

In your Fragment:

((MainFragmentActivity) getActivity()).setActionBarTitle(YOUR_TITLE);
public class MyFragment extends Fragment
    @Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);
        if (visible) {
            ...
        }
    }

Comments

comments powered by Disqus
Copyright © 2013 Andro Chen
Powered by Logdown and Greyshade
Favicon from The Noun Project