2016-10-18 13 views
1

フラグメントA、B、C、D、Eを持っています。例えば、フラグメントAが表示されている場合、私はB、C、D、Eを隠します。同じことをBをクリックするとフラグメント、A、C、D、Eが隠されます。戻るボタンを押して、私が望むやり方で機能させるようにしてください。そうすれば、ユーザーが別のフラグメントに切り替えるたびに、フラグメントをバックスタックに追加します。しかし、私が抱えている問題は、戻るボタンを押すと、以前のすべての断片がすべて一度に表示されるということです。電話で狂っているように見えますが、とにかく、この問題をどのように修正できるかアドバイスできますか?すべてのフラグメントは、Androidで戻るボタンが押されたときに表示されます

私のコードのスニペット:

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.isAdded())" へ "" もし(mUserFavoritesFragment.isVisible())」

関連する問題