我正在为片段使用 fragmenttabhost 。在一个片段中有两个子片段。当从子片段更改片段时工作正常。但是,当转到其他选项卡并返回时出错
public class MainActivity extends FragmentActivity implements
OnTabChangeListener {
private FragmentTabHost mTabHost;
private HashMap<String, Stack<Fragment>> mStacks;
private String mCurrentTab;
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.setOnTabChangedListener(this);
mStacks = new HashMap<String, Stack<Fragment>>();
mStacks.put(AppConstants.DEAL_FRAGMENT, new Stack<Fragment>());
mStacks.put(AppConstants.CONN_FRAGMENT, new Stack<Fragment>());
mStacks.put(AppConstants.PROFILE_FRAGMENT, new Stack<Fragment>());
mStacks.put(AppConstants.MORE_FRAGMENT, new Stack<Fragment>());
initializeTabs();
}
private void initializeTabs() {
Bundle b = new Bundle();
b.putString("key", AppConstants.DEAL_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.DEAL_FRAGMENT)
.setIndicator("Deal"),
DealFragment.class, b);
//
b = new Bundle();
b.putString("key", AppConstants.CONN_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.CONN_FRAGMENT)
.setIndicator("Conn"),
ConnFragment.class, b);
b = new Bundle();
b.putString("key", AppConstants.PROFILE_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.PROFILE_FRAGMENT)
.setIndicator("Profile"),
ProfileFragment.class, b);
b = new Bundle();
b.putString("key", AppConstants.MORE_FRAGMENT);
mTabHost.addTab(mTabHost.newTabSpec(AppConstants.MORE_FRAGMENT)
.setIndicator("More"),
MoreFragment.class, b);
}
public void pushFragments(String tag, Fragment fragment,
boolean shouldAnimate, boolean shouldAdd) {
/*if (shouldAdd)
mStacks.get(tag).push(fragment);*/
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
}
@Override
public void onTabChanged(String tabId) {
mCurrentTab = tabId;
Log.d("Tabid", "" + mCurrentTab);
if (mStacks.get(tabId).size() == 0) {
if (tabId.equals(AppConstants.DEAL_FRAGMENT)) {
pushFragments(tabId, new DealFragment(), false, true);
} else if (tabId.equals(AppConstants.CONN_FRAGMENT)) {
pushFragments(tabId, new ConnFragment(), false, true);
} else if (tabId.equals(AppConstants.PROFILE_FRAGMENT)) {
pushFragments(tabId, new ProfileFragment(), false, true);
} else if (tabId.equals(AppConstants.MORE_FRAGMENT)) {
pushFragments(tabId, new MoreFragment(), false, true);
}
} else {
pushFragments(tabId, mStacks.get(tabId).lastElement(), false, false);
}
}
public void popFragments() {
Fragment fragment = mStacks.get(mCurrentTab).elementAt(
mStacks.get(mCurrentTab).size() - 2);
mStacks.get(mCurrentTab).pop();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
}
@Override
public void onBackPressed() {
if(((BaseFragment)mStacks.get(mCurrentTab).lastElement()).onBackPressed() == false){
if(mStacks.get(mCurrentTab).size() == 1){
super.onBackPressed(); // or call finish..
}else{
popFragments();
}
}else{
}
}
}
公共类 DealFragment 扩展 BaseFragment 实现 OnTabChangeListener {
private FragmentTabHost mTabHost;
private String mCurrentTab;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.id.menu_settings);
mTabHost.setOnTabChangedListener(this);
initializeChildTabHost();
return mTabHost;
}
private void initializeChildTabHost() {
Bundle b = new Bundle();
b.putString("key", AppConstants.LIST_DEAL_FRAGMENT);
mTabHost.addTab(
mTabHost.newTabSpec(AppConstants.LIST_DEAL_FRAGMENT)
.setIndicator(
"List"),
CategoryListFragment.class, b);
//
b = new Bundle();
b.putString("key", AppConstants.CATEGORY_DEAL_FRAGMENT);
mTabHost.addTab(
mTabHost.newTabSpec(AppConstants.CATEGORY_DEAL_FRAGMENT).setIndicator(
"Category"),
CategoryFragment.class, b);
}
@Override
public void onTabChanged(String tabId) {
mCurrentTab = tabId;
Log.d("Current Child Tab", ""+mCurrentTab);
}
}
公共类 CategoryListFragment 扩展 BaseFragment 实现 OnClickListener{
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout, container, false);
button = (Button)view.findViewById(R.id.addNewFrag);
button.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
if(v==button){
mActivity.pushFragments(AppConstants.LIST_DEAL_FRAGMENT, new InviteFrndFragment(), true, true);
}
}
}
公共类 BaseFragment 扩展片段 { public MainActivity mActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (MainActivity) this.getActivity();
}
public boolean onBackPressed() {
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}