在Android中按下后退按钮时显示所有片段

时间:2016-10-18 20:53:44

标签: android fragment back

我有片段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;
        }
    }

1 个答案:

答案 0 :(得分:0)

更改你的行:" if(mUserFavoritesFragment.isAdded())" to"" if(mUserFavoritesFragment.isVisible())"

相关问题