2016-10-18 4 views
1

단편 A, B, C, D, E가 있습니다. 단편 사이를 전환하기 위해 표시 및 숨기기 구현을 사용하고 있습니다. 예를 들어, 조각 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; 
     } 
    } 

답변

0

변경 youe는 라인 : "을"경우 (mUserFavoritesFragment.isVisible()) "

"(mUserFavoritesFragment.isAdded()) 만약 "
관련 문제