我有一个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
}
}
}
我想要每个选项卡,如果选中该选项卡,该选项卡的片段将被推送到堆栈。因此,当我按下后退按钮时,我将返回上一个片段而不是活动。
所以我的猜测是@Override我的HomeActivity中的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()
总是具有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()
方法,则片段的内容将变为空。此选项卡的图标也会在最后选择的选项卡上保持选中状态。