1

I am studying on the ActionBar and saw this abstract method

public abstract Tab newTab();

However the implementation of newTab is only seen in ActionBarImpl.java.

From android http://developer.android.com/reference/android/support/v4/view/ViewPager.html

It is showed that

 final ActionBar bar = getActionBar();
 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
 bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

 mTabsAdapter = new TabsAdapter(this, mViewPager);
 mTabsAdapter.addTab(bar.newTab().setText("Simple"), CountingFragment.class, null);

So, where is the bar.newTab() actually instantiated?

I saw a related post What class should I extend, AcionBar or ActionBarImpl? but it doesn't seem to answer my question directly.

Community
  • 1
  • 1
ken
  • 13,869
  • 6
  • 42
  • 36

1 Answers1

1

The ActionBar class is an abstract class, like you already found out. It's actually implemented in the ActionBarImp class. Everything related to the ActionBar will be instantiated inside the Activity class. getActionBar() will return this implementation of the ActionBar:

/**
 * Retrieve a reference to this activity's ActionBar.
 *
 * @return The Activity's ActionBar, or null if it does not have one.
 */
public ActionBar getActionBar() {
    initActionBar();
    return mActionBar;
}

/**
 * Creates a new ActionBar, locates the inflated ActionBarView,
 * initializes the ActionBar with the view, and sets mActionBar.
 */
private void initActionBar() {
    [...]
    mActionBar = new ActionBarImpl(this);
    [...]
}

Source.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • Thanks for the answer! Switching from c programming, I am still exploring java, just curious why it downcast it to ActionBar, instead of using ActionBarImpl? Isn't it is more obvious if final ActionBarImpl bar = getActionBar(); is used? – ken Jan 11 '14 at 12:10