我在一个活动中都持有多个片段。在特定的情况下,当我在片段B上点击后退按钮时,它带我回到片段A,这是正确的,但是似乎片段B的布局与片段A的布局交织在一起。或来自片段B的EditText可以在片段A上使用,但是来自片段B的某些对象位于片段A的对象之上,而其他对象位于后面。
这是我用来将每个Fragment放在屏幕上的代码以及我的onBackPressed()方法:
片段A的代码:
public void showBetOddsFragment() {
mOddsFragment = new BetOddsFragment();
Bundle bundle = new Bundle();
bundle.putString("NAME", mObject.getName());
if(mSelectedBetType.hasGoal()) {
bundle.putFloat("GOAL", mGoal);
}
bundle.putString("INTERVAL", getIntervalDescription());
bundle.putString("BET_TYPE", mSelectedBetType.getName());
mOddsFragment.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.contentContainer, mOddsFragment, "BET_ODDS")
.addToBackStack(null)
.commitAllowingStateLoss();
manager.executePendingTransactions();
}
片段B的代码:
public void showAddFunds(DepositCheckout checkout) {
String checkoutStr = (new Gson()).toJson(checkout);
DepositCheckoutFragment fragment = new DepositCheckoutFragment();
Bundle arguments = new Bundle();
arguments.putString("CHECKOUT", checkoutStr);
fragment.setArguments(arguments);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.contentContainer, fragment, "DEPOSIT_CHECKOUT")
.commitAllowingStateLoss();
manager.executePendingTransactions();
}
onBackPressed()的代码:
@Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed() - " + getSupportFragmentManager().getBackStackEntryCount());
int count = getSupportFragmentManager().getBackStackEntryCount();
Utils.hideKeyboard(this);
hideProgressBar();
if(count > 1) {
super.onBackPressed();
} else {
finish();
}
}