Java Code Examples for android.support.v4.app.FragmentTransaction#hide()

The following examples show how to use android.support.v4.app.FragmentTransaction#hide() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwipeBackFragment.java    From SwipeBackFragment with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        boolean isSupportHidden = savedInstanceState.getBoolean(SWIPEBACKFRAGMENT_STATE_SAVE_IS_HIDDEN);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (isSupportHidden) {
            ft.hide(this);
        } else {
            ft.show(this);
        }
        ft.commit();
    }

    mNoAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.no_anim);
    onFragmentCreate();
}
 
Example 2
Source File: MainActivity.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 添加或者显示 fragment
 *
 * @param transaction
 * @param fragment
 */
private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    //当前的fragment就是点击切换的目标fragment,则不用操作
    if (currentFragment == fragment) {
        return;
    }

    Fragment willCloseFragment = currentFragment;//上一个要切换掉的碎片
    currentFragment = fragment;//当前要显示的碎片

    if (willCloseFragment != null) {
        transaction.hide(willCloseFragment);
    }
    if (!fragment.isAdded()) { // 如果当前fragment未被添加,则添加到Fragment管理器中
        transaction.add(R.id.fl_main_body, currentFragment).commitAllowingStateLoss();
    } else {
        transaction.show(currentFragment).commitAllowingStateLoss();
    }
}
 
Example 3
Source File: NavHelper.java    From MaoWanAndoidClient with Apache License 2.0 6 votes vote down vote up
public boolean performClickMenuFragment(int menuId){
        if (tabFragments.get(menuId)!= null){
            FragmentTransaction ft = fragmentManager.beginTransaction();
            Fragment currentFragment = tabFragments.get(menuId);
            Fragment lastFragment = tabFragments.get(lastIndexId);
            lastIndexId = menuId;
            ft.hide(lastFragment);
            if (!currentFragment.isAdded()) {
                fragmentManager.beginTransaction().remove(currentFragment).commit();
                ft.add(containerId, currentFragment);
            }
            ft.show(currentFragment);
            ft.commitAllowingStateLoss();
            return true;
        }else {
            return false;
        }
}
 
Example 4
Source File: ListHostsActivity.java    From hosts-editor-android with Apache License 2.0 6 votes vote down vote up
@Subscribe
public void onLoadingEvent(LoadingEvent event) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    mLoadingMsg.setText(event.getMessage(this));
    if (event.isLoading()) {
        Timber.d("Start loading");
        if (mSearchItem != null) {
            setSearchQuery(STR_EMPTY);
            mSearchItem.collapseActionView();
        }

        mProgressBar.setVisibility(View.VISIBLE);
        mLoadingMsg.setVisibility(View.VISIBLE);
        ft.hide(mFragment);
    } else {
        Timber.d("Stop loading");
        mProgressBar.setVisibility(View.GONE);
        mLoadingMsg.setVisibility(View.GONE);
        ft.show(mFragment);
    }
    ft.commitAllowingStateLoss();
}
 
Example 5
Source File: MainActivity.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * 切换显示当前Fragment
 *
 * @param index
 */
private void showCurrentFragment(int index) {
    if (currentPosition != index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.hide(fragments[currentPosition]);
        if (!fragments[index].isAdded()) {
            ft.add(R.id.container, fragments[index]);
        }
        ft.show(fragments[index]).commit();
        btns[currentPosition].setSelected(false);
        btns[index].setSelected(true);
        scaleView();
        currentPosition = index;
        setCurrentTitle();
    }
}
 
Example 6
Source File: FragmentCompat.java    From tysq-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 初始化fragment,list
 *
 * @param fragmentManager
 * @param containerId
 * @param showPosition
 * @param fragments
 */
public static void init(FragmentManager fragmentManager,
                        int containerId,
                        int showPosition,
                        List<Fragment> fragments) {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    int size = fragments.size();
    for (int i = 0; i < size; i++) {
        Fragment fragment = fragments.get(i);

        String toName = fragment.getClass().getName();
        ft.add(containerId, fragment, toName);

        if (i != showPosition) {
            ft.hide(fragment);
        } else {
            fragment.onHiddenChanged(false);
        }
    }
    ft.commit();
}
 
Example 7
Source File: BaseActivity.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
protected void hideFragmentByTag(String... tags) {

        if (tags.length <= 0) {
            return;
        }

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        for (String tag : tags) {
            if (TextUtils.isEmpty(tag)) {
                continue;
            }
            Fragment fragment = manager.findFragmentByTag(tag);
            if (fragment != null) {
                transaction.hide(fragment);
            }
        }
        transaction.commit();
    }
 
Example 8
Source File: MainActivity.java    From myapplication with Apache License 2.0 6 votes vote down vote up
/**
 * 隐藏Fragment
 *
 * @param fragmentTransaction fragmentTransaction
 */
private void hideFragments(FragmentTransaction fragmentTransaction) {
    if (fg1 != null) {
        fragmentTransaction.hide(fg1);
    }

    if (fg2 != null) {
        fragmentTransaction.hide(fg2);
    }

    if (fg3 != null) {
        fragmentTransaction.hide(fg3);
    }

    if (fg4 != null) {
        fragmentTransaction.hide(fg4);
    }
}
 
Example 9
Source File: MainActivity.java    From GankGirl with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 隐藏Fragment
 *
 * @param fragmentTransaction 事务
 */
private void hideFragments(FragmentTransaction fragmentTransaction) {
    if (mGankTadPageFragment != null) {
        fragmentTransaction.hide(mGankTadPageFragment);
    }

    if (mReadTadPageFragment != null) {
        fragmentTransaction.hide(mReadTadPageFragment);
    }

    if (mGirlFragment != null) {
        fragmentTransaction.hide(mGirlFragment);
    }

    if (mVideoFragment != null) {
        fragmentTransaction.hide(mVideoFragment);
    }
}
 
Example 10
Source File: MainActivity.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void hideFragment(FragmentTransaction transaction) {
    if (homeFragment != null) {
        transaction.hide(homeFragment);
    }
    if (friendsListFragment != null) {
        transaction.hide(friendsListFragment);
    }
    if (newsFragment != null) {
        transaction.hide(newsFragment);
    }
    if (applicationFragment != null) {
        transaction.hide(applicationFragment);
    }
}
 
Example 11
Source File: FragmentTabHost.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
        TabInfo newTab = null;
        for (int i = 0; i < mTabs.size(); i++) {
            TabInfo tab = mTabs.get(i);
            if (tab.tag.equals(tabId)) {
                newTab = tab;
            }
        }
        if (newTab == null) {
            throw new IllegalStateException("No tab known for tag " + tabId);
        }
        if (mLastTab != newTab) {
            if (ft == null) {
                ft = mFragmentManager.beginTransaction();
            }
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
//                    ft.detach(mLastTab.fragment);
                    ft.hide(mLastTab.fragment);
                }
            }
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext, newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
//                    ft.attach(newTab.fragment);
                ft.show(newTab.fragment);
            }

            mLastTab = newTab;
        }
        return ft;
    }
 
Example 12
Source File: BaseActivity.java    From VideoMeeting with Apache License 2.0 5 votes vote down vote up
public void switchFragment(@NonNull int fContentId,@NonNull String tag, @NonNull CreateFragmentCallback callback) {
        String newTag = makeFragmentName(fContentId, tag);
        Fragment to = callback.getFragmentManager(this, tag).findFragmentByTag(newTag);
        if (to == null) {
            L.d("create fragment " + tag);
            to = callback.onCreateFragment(tag);
        }
        if (mCurFragment != to) {
            FragmentTransaction transaction = callback.onGetTransaction(this, tag);
            if (mCurFragment == null) {
                List<Fragment> fragments = callback.getFragmentManager(this, tag).getFragments();
                if (fragments != null) { // 是内存重启
                    for (int i = 0; i < fragments.size(); i++) {
                        L.d("hide all fragments");
                        transaction.hide(fragments.get(i));
                    }
                }
            }
            if (!to.isAdded()) {    // 先判断是否被add过
                if (mCurFragment != null) {
                    transaction.hide(mCurFragment);
                }
                L.d("add fragment " + tag);
                transaction.add(fContentId, to, newTag).commit(); // 隐藏当前的fragment,add下一个到Activity中
            } else {
                if (mCurFragment != null) {
                    transaction.hide(mCurFragment);
                }
                L.d("show fragment " + tag);
                transaction.show(to).commit(); // 隐藏当前的fragment,显示下一个
            }
//            callback.getFragmentManager(this, tag).executePendingTransactions();
            mCurFragment = to;
        } else {
            L.d("mCurFragment == to");
        }
    }
 
Example 13
Source File: MainTimeLineActivity.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
private void initFragments() {
    Fragment timeLineFragment = getMainTimeLineFragment();
    Fragment favFragment = getFavFragment();

    Fragment hotWeiboFragment = getHotWeiboViewPagerFragment();

    Fragment hotHuatiFragment = getHotHuaTiViewPagerFragment();

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    if (!timeLineFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, timeLineFragment, MainTimeLineFragment.class.getName());
        fragmentTransaction.hide(timeLineFragment);
    }

    if (!favFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, favFragment, MyFavListFragment.class.getName());
        fragmentTransaction.hide(favFragment);
    }

    if (!hotWeiboFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, hotWeiboFragment, HotWeiboViewPagerFragment.class.getName());
        fragmentTransaction.hide(hotWeiboFragment);
    }

    if (!hotHuatiFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, hotHuatiFragment, HotHuaTiViewPagerFragment.class.getName());
        fragmentTransaction.hide(hotHuatiFragment);
    }

    if (!fragmentTransaction.isEmpty()) {
        fragmentTransaction.commit();
        getSupportFragmentManager().executePendingTransactions();
    }
}
 
Example 14
Source File: MainTabAct.java    From Alibaba-Android-Certification with MIT License 5 votes vote down vote up
/**
 * Fragment切换方法
 *
 * @Date 2014-3-7
 * @param hideFragment 要隐藏的Fragment 可以为null
 * @param startFragment 要启动的Fragment
 * @return Boolean 是否切换了
 */
public  boolean addFragmentContainer(BaseFragment hideFragment, BaseFragment startFragment, int container) {
    if (contentFragment != null) {
        //当前Fragment与启动发Fragment一致不执行任何操作
        if (contentFragment.getTagKey().equals(startFragment.getTagKey())) {
            //不能直接用startFragment和contentFragment对象地址比较,否则异常闪退的话会出现两层一样的界面
            return false;
        }
    }
    //记录当前Fragment
    contentFragment = startFragment;
    //开启一个事物
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    String tagKey = startFragment.getTagKey();
    //不为空的话隐藏
    if (hideFragment != null) {
        transaction.hide(hideFragment);
    }
    //已经添加的话显示
    if (startFragment.isAdded()) {
        transaction.show(startFragment);
        //第一次添加,并保存tagKey(Fragment类名),可以用getSupportFragmentManager()直接根据tagKey名取出该Fragment进行操作
    } else {
        transaction.add(container, startFragment, tagKey);
    }
    transaction.commitAllowingStateLoss();

    return true;
}
 
Example 15
Source File: MainActivity.java    From TreeMenu with Apache License 2.0 5 votes vote down vote up
private void initFragments() {
    fragmentMap.put(MenuInScrollViewFragment.class.getSimpleName(), MenuInScrollViewFragment.newInstance());
    fragmentMap.put(InitSelectedMenuFragment.class.getSimpleName(), InitSelectedMenuFragment.newInstance());
    fragmentMap.put(AboutMeFragment.class.getSimpleName(), AboutMeFragment.newInstance());

    mFragmentManager = getSupportFragmentManager();
    FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
    for (Fragment fragment : fragmentMap.values()) {
        mTransaction.add(R.id.container, fragment);
        mTransaction.hide(fragment);
    }
    mTransaction.commit();
}
 
Example 16
Source File: VoiceInputFragment.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
@Override
public void switchView(boolean show) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    if (show)
        transaction.show(this);
    else
        transaction.hide(this);
    transaction.commit();
}
 
Example 17
Source File: BaseActivity.java    From JianDanRxJava with Apache License 2.0 4 votes vote down vote up
protected void hideFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.hide(fragment);
    transaction.commit();
}
 
Example 18
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 4 votes vote down vote up
/**
     * 操作fragment
     *
     * @param fragmentManager fragment管理器
     * @param srcFragment     源fragment
     * @param destFragment    目标fragment
     * @param type            操作类型
     * @param sharedElements  共享元素
     * @return destFragment
     */
    private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                            Fragment srcFragment,
                                            @NonNull Fragment destFragment,
                                            int type,
                                            SharedElement... sharedElements) {
        if (srcFragment == destFragment)
            return null;
        if (srcFragment != null && srcFragment.isRemoving()) {
//            Log.e(srcFragment.getClass().getSimpleName() + " is isRemoving");
            return null;
        }
        String name = destFragment.getClass().getSimpleName();
        Bundle args = destFragment.getArguments();

        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (sharedElements == null || sharedElements.length == 0) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        } else {
            for (SharedElement element : sharedElements) {// 添加共享元素动画
                ft.addSharedElement(element.sharedElement, element.name);
            }
        }
        switch (type) {
            case TYPE_ADD_FRAGMENT:
                if (srcFragment != null)
                    ft.hide(srcFragment);
                if (destFragment.isAdded())
                    break;
                ft.add(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_HIDE))
                    ft.hide(destFragment);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_REMOVE_FRAGMENT:
                ft.remove(destFragment);
                break;
            case TYPE_REMOVE_TO_FRAGMENT:
                List<Fragment> fragments = getFragments(fragmentManager);
                for (int i = fragments.size() - 1; i >= 0; --i) {
                    Fragment fragment = fragments.get(i);
                    if (fragment == destFragment) {
                        if (srcFragment != null)
                            ft.remove(fragment);
                        break;
                    }
                    ft.remove(fragment);
                }
                break;
            case TYPE_REPLACE_FRAGMENT:
                ft.replace(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_POP_ADD_FRAGMENT:
                popFragment(fragmentManager);
                ft.add(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_HIDE_FRAGMENT:
                ft.hide(destFragment);
                break;
            case TYPE_SHOW_FRAGMENT:
                ft.show(destFragment);
                break;
            case TYPE_HIDE_SHOW_FRAGMENT:
                ft.hide(srcFragment).show(destFragment);
                break;
        }
        ft.commitAllowingStateLoss();
        return destFragment;
    }
 
Example 19
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                        Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        int type,
                                        SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_ADD_FRAGMENT:
            if (srcFragment != null) ft.hide(srcFragment);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 20
Source File: SubsonicActivity.java    From Popeens-DSub with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
	super.onRestoreInstanceState(savedInstanceState);
	int size = savedInstanceState.getInt(Constants.MAIN_BACK_STACK_SIZE);
	String[] ids = savedInstanceState.getStringArray(Constants.MAIN_BACK_STACK);
	FragmentManager fm = getSupportFragmentManager();
	currentFragment = (SubsonicFragment)fm.findFragmentByTag(ids[0]);
	currentFragment.setPrimaryFragment(true);
	currentFragment.setSupportTag(ids[0]);
	supportInvalidateOptionsMenu();
	FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
	for(int i = 1; i < size; i++) {
		SubsonicFragment frag = (SubsonicFragment)fm.findFragmentByTag(ids[i]);
		frag.setSupportTag(ids[i]);
		if(secondaryContainer != null) {
			frag.setPrimaryFragment(false, true);
		}
		trans.hide(frag);
		backStack.add(frag);
	}
	trans.commit();

	// Current fragment is hidden in secondaryContainer
	if(secondaryContainer == null && !currentFragment.isVisible()) {
		trans = getSupportFragmentManager().beginTransaction();
		trans.remove(currentFragment);
		trans.commit();
		getSupportFragmentManager().executePendingTransactions();

		trans = getSupportFragmentManager().beginTransaction();
		trans.add(R.id.fragment_container, currentFragment, ids[0]);
		trans.commit();
	}
	// Current fragment needs to be moved over to secondaryContainer
	else if(secondaryContainer != null && secondaryContainer.findViewById(currentFragment.getRootId()) == null && backStack.size() > 0) {
		trans = getSupportFragmentManager().beginTransaction();
		trans.remove(currentFragment);
		trans.show(backStack.get(backStack.size() - 1));
		trans.commit();
		getSupportFragmentManager().executePendingTransactions();

		trans = getSupportFragmentManager().beginTransaction();
		trans.add(R.id.fragment_second_container, currentFragment, ids[0]);
		trans.commit();

		secondaryContainer.setVisibility(View.VISIBLE);
	}

	lastSelectedPosition = savedInstanceState.getInt(Constants.FRAGMENT_POSITION);
	if(lastSelectedPosition != 0) {
		MenuItem item = drawerList.getMenu().findItem(lastSelectedPosition);
		if(item != null) {
			item.setChecked(true);
		}
	}
	recreateSpinner();
       checkIfServerOutdated();
}