[英]All fragments show when back button is pressed in Android
我有片段A,B,C,D和E,我使用show和hide實現在片段之間切換。 因此,例如,如果正在顯示片段A,則隱藏B,C,D和E.如果我單擊B,則會隱藏片段,A,C,D和E,使用此方法我無法按下后退按鈕按照我想要的方式運行,所以現在每當用戶切換到不同的片段時,我將片段添加到后面的堆棧。 然而我遇到的問題是,當我按下后退按鈕時,所有以前的片段都會立即顯示出來,大聲笑它在手機上看起來很瘋狂,但無論如何,有人可以建議我如何解決這個問題嗎?
我的代碼片段:
public void onAccountSettingsSelected(){
// Return if the fragment is the same
if(mAccountSettingsFragment.isVisible()){
Log.d("The World of Go ", "Account Settings fragment already is visible in container, returning");
return;
}else {
secondFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (mAccountSettingsFragment.isAdded()) { // if the fragment is already in container
Log.d("The World of Go ", "Account Settings fragment already in container, re-showing the original Account Settings fragment");
secondFragmentTransaction.show(mAccountSettingsFragment);
secondFragmentTransaction.addToBackStack(null);
} else { // fragment needs to be added to frame container
Log.d("The World of Go ", "Account Settings fragment is not already in container, creating new Account Settings fragment");
Bundle args = new Bundle();
mAccountSettingsFragment.setArguments(args);
secondFragmentTransaction.add(R.id.detail_fragment_container, mAccountSettingsFragment);
secondFragmentTransaction.addToBackStack(null);
}
// Hide the User Favorites fragment
if (mUserFavoritesFragment.isAdded()) {
secondFragmentTransaction.hide(mUserFavoritesFragment);
}
// Hide the Maps fragment
if (mMapsFragment.isAdded()) {
secondFragmentTransaction.hide(mMapsFragment);
}
// Hide the Broadcast fragment
if (mBroadcastFragment.isAdded()) {
secondFragmentTransaction.hide(mBroadcastFragment);
}
// Hide the Friends fragment
if (mFriendsFragment.isAdded()) {
secondFragmentTransaction.hide(mFriendsFragment);
}
// Hide the Login fragment
if (mLoginFragment.isAdded()) {
secondFragmentTransaction.hide(mLoginFragment);
}
// Commit the transaction
secondFragmentTransaction.commit();
mIsUserFavoritesOpen = false;
mIsMapsOpen = false;
mIsFriendsOpen = false;
}
}
public void onUserFavoritesSelected(){
// Return if the fragment is the same
if(mUserFavoritesFragment.isVisible()){
Log.d("The World of Go ", "User Favorites fragment already is visible in container, returning");
return;
}else {
secondFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (mUserFavoritesFragment.isAdded()) { // if the fragment is already in container
Log.d("The World of Go ", "User Favorites fragment already in container, re-showing the original User Favorites fragment");
secondFragmentTransaction.show(mUserFavoritesFragment);
secondFragmentTransaction.addToBackStack(null);
} else { // fragment needs to be added to frame container
Log.d("The World of Go ", "User Favorites fragment is not already in container, creating new User Favorites fragment");
Bundle args = new Bundle();
mUserFavoritesFragment.setArguments(args);
secondFragmentTransaction.add(R.id.detail_fragment_container, mUserFavoritesFragment);
secondFragmentTransaction.addToBackStack(null);
}
// Hide the Maps fragment
if (mMapsFragment.isAdded()) {
secondFragmentTransaction.hide(mMapsFragment);
}
// Hide the Account Settings fragment
if (mAccountSettingsFragment.isAdded()) {
secondFragmentTransaction.hide(mAccountSettingsFragment);
}
// Hide the Broadcast fragment
if (mBroadcastFragment.isAdded()) {
secondFragmentTransaction.hide(mBroadcastFragment);
}
// Hide the Friends fragment
if (mFriendsFragment.isAdded()) {
secondFragmentTransaction.hide(mFriendsFragment);
}
// Hide the Login fragment
if (mLoginFragment.isAdded()) {
secondFragmentTransaction.hide(mLoginFragment);
}
// Commit the transaction
secondFragmentTransaction.commit();
mIsUserFavoritesOpen = true;
mIsMapsOpen = false;
mIsFriendsOpen = false;
}
}
public void onMapsSelected(){
// Return if the fragment is the same
if(mMapsFragment.isVisible()){
Log.d("The World of Go ", "Maps fragment already is visible in container, returning");
return;
}else {
secondFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (mMapsFragment.isAdded()) { // if the fragment is already in container
Log.d("The World of Go ", "Maps fragment already in container, re-showing the original maps fragment");
secondFragmentTransaction.show(mMapsFragment);
secondFragmentTransaction.addToBackStack(null);
} else { // fragment needs to be added to frame container
Log.d("The World of Go ", "Maps fragment is not already in container, creating new maps fragment");
Bundle args = new Bundle();
mMapsFragment.setArguments(args);
secondFragmentTransaction.add(R.id.detail_fragment_container, mMapsFragment);
secondFragmentTransaction.addToBackStack(null);
}
// Hide the User Favorites fragment
if (mUserFavoritesFragment.isAdded()) {
secondFragmentTransaction.hide(mUserFavoritesFragment);
}
// Hide the Account settings fragment
if (mAccountSettingsFragment.isAdded()) {
secondFragmentTransaction.hide(mAccountSettingsFragment);
}
// Hide the Broadcast fragment
if (mBroadcastFragment.isAdded()) {
secondFragmentTransaction.hide(mBroadcastFragment);
}
// Hide the Friends fragment
if (mFriendsFragment.isAdded()) {
secondFragmentTransaction.hide(mFriendsFragment);
}
// Hide the Login fragment
if (mLoginFragment.isAdded()) {
secondFragmentTransaction.hide(mLoginFragment);
}
// Commit the transaction
secondFragmentTransaction.commit();
mIsUserFavoritesOpen = false;
mIsMapsOpen = true;
mIsFriendsOpen = false;
}
}
public void onFriendSelected(){
// Return if the fragment is the same
if(mFriendsFragment.isVisible()){
Log.d("The World of Go ", "Friends fragment already is visible in container, returning");
return;
}else {
secondFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (mFriendsFragment.isAdded()) { // if the fragment is already in container
Log.d("The World of Go ", "Friends fragment already in container, re-showing the original Friends fragment");
secondFragmentTransaction.show(mFriendsFragment);
secondFragmentTransaction.addToBackStack(null);
} else { // fragment needs to be added to frame container
Log.d("The World of Go ", "Friends fragment is not already in container, creating new Friends fragment");
Bundle args = new Bundle();
mFriendsFragment.setArguments(args);
secondFragmentTransaction.add(R.id.detail_fragment_container, mFriendsFragment);
secondFragmentTransaction.addToBackStack(null);
}
// Hide User Favorites fragment
if (mUserFavoritesFragment.isAdded()) {
secondFragmentTransaction.hide(mUserFavoritesFragment);
}
// Hide the Maps fragment
if (mMapsFragment.isAdded()) {
secondFragmentTransaction.hide(mMapsFragment);
}
// Hide the Broadcast fragment
if (mBroadcastFragment.isAdded()) {
secondFragmentTransaction.hide(mBroadcastFragment);
}
// Hide the Account Settings fragment
if (mAccountSettingsFragment.isAdded()) {
secondFragmentTransaction.hide(mAccountSettingsFragment);
}
// Hide the Login fragment
if (mLoginFragment.isAdded()) {
secondFragmentTransaction.hide(mLoginFragment);
}
// Commit the transaction
secondFragmentTransaction.commit();
mIsUserFavoritesOpen = false;
mIsMapsOpen = false;
mIsFriendsOpen = true;
}
}
public void onBroadcastSelected(){
// Return if the fragment is the same
if(mBroadcastFragment.isVisible()){
Log.d("The World of Go ", "Broadcast fragment already is visible in container, returning");
return;
}else {
secondFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (mBroadcastFragment.isAdded()) { // if the fragment is already in container
Log.d("The World of Go ", "Broadcast fragment already in container, re-showing the original Broadcast fragment");
secondFragmentTransaction.show(mBroadcastFragment);
secondFragmentTransaction.addToBackStack(null);
} else { // fragment needs to be added to frame container
Log.d("The World of Go ", "Broadcast fragment is not already in container, creating new Broadcast fragment");
Bundle args = new Bundle();
mBroadcastFragment.setArguments(args);
secondFragmentTransaction.add(R.id.detail_fragment_container, mBroadcastFragment);
secondFragmentTransaction.addToBackStack(null);
}
// Hide User Favorites fragment
if (mUserFavoritesFragment.isAdded()) {
secondFragmentTransaction.hide(mUserFavoritesFragment);
}
// Hide User Favorites fragment
if (mMapsFragment.isAdded()) {
secondFragmentTransaction.hide(mMapsFragment);
}
// Hide User Favorites fragment
if (mFriendsFragment.isAdded()) {
secondFragmentTransaction.hide(mFriendsFragment);
}
// Hide User Favorites fragment
if (mAccountSettingsFragment.isAdded()) {
secondFragmentTransaction.hide(mAccountSettingsFragment);
}
// Hide User Favorites fragment
if (mLoginFragment.isAdded()) {
secondFragmentTransaction.hide(mLoginFragment);
}
// Commit the transaction
secondFragmentTransaction.commit();
mIsUserFavoritesOpen = false;
mIsMapsOpen = false;
mIsFriendsOpen = false;
}
}
改變你的行:“if(mUserFavoritesFragment.isAdded())”到“”if(mUserFavoritesFragment.isVisible())“
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.