我有一个 SherlockFragmentActivity 负责添加选项卡并将侦听器添加到这些选项卡。我以以下方式添加标签:
public class HomeActivity extends SherlockFragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Styled);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// First add some tabs and give them explaining names
ActionBar.Tab homeTab = getSupportActionBar()
.newTab()
.setIcon(R.drawable.home_unselected)
.setTabListener(new TabListener<SherlockFragment>(this, "home", HomeFragment.class));
ActionBar.Tab profileTab = getSupportActionBar()
.newTab()
.setIcon(R.drawable.profile_unselected)
.setTabListener(new TabListener<SherlockFragment>(this, "profile", ProfileFragment.class));
getSupportActionBar().addTab(homeTab);
getSupportActionBar().addTab(profileTab);
}
public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener
{
private SherlockFragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<?> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<?> clz)
{
mActivity = (SherlockFragmentActivity) activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
// Check if the fragment has already been initialised
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
int resID = mActivity.getApplicationContext().getResources().getIdentifier(mTag + "_selected" , "drawable", mActivity.getPackageName());
Drawable image = mActivity.getApplicationContext().getResources().getDrawable(resID);
tab.setIcon(image);
if (mFragment != null)
{
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
else if (preInitializedFragment != null)
{
mFragment = preInitializedFragment;
ft.attach(mFragment);
}
else
{
// Not found, so instantiate and add it to the activity
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
}
}
@Override
public void onTabUnselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
int resID = mActivity.getApplicationContext().getResources().getIdentifier(mTag + "_unselected" , "drawable", mActivity.getPackageName());
Drawable image = mActivity.getApplicationContext().getResources().getDrawable(resID);
tab.setIcon(image);
}
}
@Override
public void onTabReselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
我希望对于每个选项卡,如果选择了该选项卡,则该选项卡的片段被推送到堆栈中。所以当我按下后退按钮时,我会返回上一个片段而不是活动。
所以我的猜测是像这样在我的 HomeActivity 中 @Override 我的 onBackPressed :
@Override
public void onBackPressed() {
// initialize variables
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// check to see if stack is empty
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
ft.commit();
}
else {
super.onBackPressed();
}
return;
}
但事实证明fm.getBackStackEntryCount()
ALWAYS 的值是0
。所以我的片段永远不会添加到堆栈中。他们为什么不呢?我错过了什么。
编辑 :
尚未完全解决,但我现在有以下代码:
@Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
// Check if the fragment has already been initialised
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
int resID = mActivity.getApplicationContext().getResources().getIdentifier(mTag + "_selected" , "drawable", mActivity.getPackageName());
Drawable image = mActivity.getApplicationContext().getResources().getDrawable(resID);
tab.setIcon(image);
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
if (mFragment != null)
{
// If it exists, simply attach it in order to show it
transaction.add(android.R.id.content, mFragment);
transaction.addToBackStack(null);
transaction.commit();
}
else if (preInitializedFragment != null)
{
mFragment = preInitializedFragment;
transaction.attach(mFragment);
transaction.addToBackStack(null);
transaction.commit();
}
else
{
// Not found, so instantiate and add it to the activity
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
transaction.add(android.R.id.content, mFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
但是现在,如果popBackStack()
触发该方法,片段的内容就会变为空。此外,选项卡的图标在最后选择的选项卡上保持选中状态。