这看起来像一个简单的问题,但我只想知道,
我们是否必须删除onClickListener
或在我的情况下TabListener
说当活动被破坏或暂停时?
例如这是我的代码:
package code.tabnav;
import adapter.SectionsPagerAdapter;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.example.tabnav.R;
public class Home extends FragmentActivity {
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ActionBar.TabListener tabListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
/* On Create method */
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mViewPager = (ViewPager) findViewById(R.id.pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager(), this);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(1);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
});
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction ft) {
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction ft) {
}
};
addNewTab("Home", tabListener);
addNewTab("Latest", tabListener);
addNewTab("Find", tabListener);
addNewTab("Download", tabListener);
}
private final void addNewTab(String name, ActionBar.TabListener tabListener) {
getActionBar().addTab(
getActionBar().newTab().setText(name)
.setTabListener(tabListener));
}
}
任何信息将不胜感激,谢谢!