我正在添加fragment
,如下:
@OnClick(R.id.lnNews)
void newsList() {
String tagName = returnStatusFragment(getString(R.string.news_list_fragment));
if (!TextUtils.isEmpty(tagName) && tagName.equals(getString(R.string.news_list_fragment))) {
frameWorkTableFragment.setVisibility(View.VISIBLE);
if (getActivity() != null) {
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment news = fragmentManager.findFragmentByTag(getString(R.string.news_list_fragment));
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_work_table_fragment,
news,
getString(R.string.news_list_fragment)).commit();
}
//Fragment is opened
} else {
if (getActivity() != null) {
frameWorkTableFragment.setVisibility(View.VISIBLE);
NewsListFragment newsListFragment = new NewsListFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_work_table_fragment,
newsListFragment,
getString(R.string.news_list_fragment))
.addToBackStack(getString(R.string.news_list_fragment))
.commit();
}
}
}
在Fragment
我写doBackPress
就像吼叫:
@Override
public void doBackTabZero(String nameFragment) {
android.support.v4.app.FragmentManager fragmentManagers = getActivity().getSupportFragmentManager();
if (fragmentManagers.getBackStackEntryCount() > 1) {
fragmentManagers.popBackStackImmediate();
} else {
if (nameFragment.equals("SignsFragment")) {
BusDisplayStatusFrameLayout busDisplayStatusFrameLayout = new BusDisplayStatusFrameLayout();
busDisplayStatusFrameLayout.setDisplayStatusFrameLayout(1);
EventBus.getDefault().post(busDisplayStatusFrameLayout);
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentManager.popBackStackImmediate();
fragmentTransaction.remove(SignsFragment.this);
fragmentTransaction.commit();
}
}
}
但是让我经常在这一行中崩溃:
fragmentManagers.popBackStackImmediate();
有了这条消息:
java.lang.IllegalStateException: Fragment already added: NewsFragment{aed4cd1 #3 id=0x7f0a006e NewsFragment}
在activity
我的doBack
就像吼叫:
private Fragment getCurrentFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
return fragmentManager.findFragmentByTag(fragmentTag);
}
然后:
@Override
public void onBackPressed() {
int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
if (backStackEntryCount > 0) {
if (onBackPressedListenerTabWorkTable != null || onBackPressedListenerTabMou != null) {
if (getCurrentFragment().getTag() != null && !TextUtils.isEmpty(getCurrentFragment().getTag())) {
if (getCurrentFragment().getTag().equals("EventsFragment")
|| getCurrentFragment().getTag().equals("WebViewListClassFragment")
|| getCurrentFragment().getTag().equals("SignsFragment")
|| getCurrentFragment().getTag().equals("NewsListFragment")) {
if (viewPagerMainActivity.getCurrentItem() != 0) {
viewPagerMainActivity.setCurrentItem(0);
}
onBackPressedListenerTabWorkTable.doBackTabZero(getCurrentFragment().getTag());
} else if (getCurrentFragment().getTag().equals("SpecialNewsMouFragment")) {
if (viewPagerMainActivity.getCurrentItem() != 1) {
viewPagerMainActivity.setCurrentItem(1);
}
onBackPressedListenerTabMou.doBackTabOne(getCurrentFragment().getTag());
}
}
}
} else {
super.onBackPressed();
onBackPressedListenerTabWorkTable = null;
onBackPressedListenerTabMou = null;
finish();
}
}