好吧,我的应用程序大约有 5000 行代码,这就是我放置 google 代码 git 存储库的原因。但我找到了解决办法!首先,在创建我的活动时,我检查我是否在我有问题的选项卡区域。如果是,我删除所有交易信息。
if(savedInstanceState != null){
int tabPosition = savedInstanceState.getInt("TAB_POSITION");
if(tabPosition == 1){
FragmentManager manager = this.getSupportFragmentManager();
for(int i = 0; i < manager.getBackStackEntryCount(); ++i) {
manager.popBackStack();
}
}
}
然后,这里是我如何重新加载所有片段,再次注册先前损坏的转换。
TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
Log.d("MainActivity","public void onTabChanged(String tabId) { called");
if(tabId.equals(TAB_A)){
pushFragments(getString(R.string.rss), fragment1rss);
}else if(tabId.equals(TAB_B)){
pushFragments(getString(R.string.worklog), fragment2worklog);
int levelChoice = WorkoutManagerSingleton.getInstance().getLevelChoice();
ListFragment nextFragment = null;
if(levelChoice > 0){
nextFragment = new Fragment2worklog_1subProg();
pushFragmentsRegisterInStack(nextFragment);
}
if(levelChoice > 1){
nextFragment = new Fragment2worklog_2week();
pushFragmentsRegisterInStack(nextFragment);
}
if(levelChoice > 2){
nextFragment = new Fragment2worklog_3day();
pushFragmentsRegisterInStack(nextFragment);
}
if(levelChoice > 3){
nextFragment = new Fragment2worklog_4exercices();
pushFragmentsRegisterInStack(nextFragment);
}
}else if(tabId.equals(TAB_C)){
pushFragments(getString(R.string.testimonials), fragment3testimonials);
}else if(tabId.equals(TAB_D)){
pushFragments(getString(R.string.recipes), fragment4recipe);
}else if(tabId.equals(TAB_E)){
pushFragments(getString(R.string.archives), fragment5archives);
}else if(tabId.equals(TAB_F)){
pushFragments(getString(R.string.book), fragment6book);
}
Log.d("MainActivity","public void onTabChanged(String tabId) { end");
}
};
/*
* adds the fragment to the FrameLayout
*/
public void pushFragments(String tag, Fragment fragment){
FragmentManager manager = this.getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment);
ft.commit();
}
public void pushFragmentsRegisterInStack(Fragment fragment){
FragmentManager manager = this.getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment);
ft.addToBackStack(null);
ft.commit();
}