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

The following examples show how to use android.support.v4.app.FragmentTransaction#show() . 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: BaseFragment.java    From AndroidBasicProject with MIT License 6 votes vote down vote up
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if(savedInstanceState != null){
        boolean isHidden = savedInstanceState.getBoolean(STATE_IS_HIDDEN);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if(isHidden){
            transaction.hide(this);
            onFragmentHide();
        } else {
            transaction.show(this);
            onFragmentShow();
        }
        transaction.commit();
    }
    register();
}
 
Example 2
Source File: BluetoothActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
@Override
public void onBluetoothClick(BluetoothDevice device) {
    if (device != null) {
        if (!lastDeviceAddress.equals(device.getAddress()))
            mService.initiativeStop();
        mService.connect(device);
        lastDeviceAddress = device.getAddress();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
        if (currentFragment.getTag().equals(LIST_FRAGMENT))
            transaction.hide(currentFragment);
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(CHAT_FRAGMENT);
        ((BluetoothChatFragment) fragment).setDeviceAddress(lastDeviceAddress);
        if (!fragment.isAdded())
            transaction.add(fragment, CHAT_FRAGMENT);
        transaction.show(fragment);
        currentFragment = fragment;
        transaction.commit();
        String subtitle = device.getName();
        if (TextUtils.isEmpty(subtitle))
            subtitle = lastDeviceAddress;
        setSubtitle(subtitle);
    }
}
 
Example 3
Source File: SocialMainActivity.java    From Social with Apache License 2.0 6 votes vote down vote up
private void clickMine(FragmentTransaction fragmentTransaction) {
    position = 3;
    resetBottomItemSelected();
    tv_mine.setSelected(true);
    if (mineFragment == null) {
        mineFragment = new MineFragment();
        if (saveInstanceState ==null) fragmentTransaction.add(R.id.ly_content, mineFragment,"mineFragment");
        else {
            mineFragment = (MineFragment)fManager.findFragmentByTag("mineFragment");
            if(mineFragment==null){
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.ly_content, mineFragment,"mineFragment");
            }else{
                fragmentTransaction.show(mineFragment);
            }
        }
    } else {
        fragmentTransaction.show(mineFragment);
    }
    fragmentTransaction.commit();
}
 
Example 4
Source File: SubsonicFragmentActivity.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void removeCurrent() {
	if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && secondaryFragment != null) {
		FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
		trans.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
		trans.remove(secondaryFragment);
		trans.show(nowPlayingFragment);
		trans.commit();

		secondaryFragment = null;
		nowPlayingFragment.setPrimaryFragment(true);
		supportInvalidateOptionsMenu();
	} else {
		super.removeCurrent();
	}
}
 
Example 5
Source File: MainActivity.java    From Awesome-WanAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * 切换fragment
 *
 * @param position 要显示的fragment的下标
 */
private void switchFragment(int position) {
    if (position >= Constants.TYPE_COLLECT) {
        mFloatingActionButton.setVisibility(View.INVISIBLE);
        mBottomNavigationView.setVisibility(View.INVISIBLE);
    } else {
        mFloatingActionButton.setVisibility(View.VISIBLE);
        mBottomNavigationView.setVisibility(View.VISIBLE);
    }
    if (position >= mFragments.size()) {
        return;
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment targetFg = mFragments.get(position);
    Fragment lastFg = mFragments.get(mLastFgIndex);
    mLastFgIndex = position;
    ft.hide(lastFg);
    if (!targetFg.isAdded()) {
        getSupportFragmentManager().beginTransaction().remove(targetFg).commitAllowingStateLoss();
        ft.add(R.id.fragment_group, targetFg);
    }
    ft.show(targetFg);
    ft.commitAllowingStateLoss();
}
 
Example 6
Source File: PhotoAlbumActivity.java    From Android with MIT License 6 votes vote down vote up
/**
 * Preview the selected images
 * @param isselect true :Preview the selected image false: Preview all images
 */
public void preViewInfos(boolean isselect, int postion) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (gridFragment != null) {
        fragmentTransaction.hide(gridFragment);
    }
    if (galleryFragment == null) {
        galleryFragment = AlbumGalleryFragment.newInstance();
        fragmentTransaction.add(R.id.framelayout, galleryFragment);
    } else {
        fragmentTransaction.show(galleryFragment);
    }

    galleryFragment.setPreViewState(isselect, postion);
    fragmentTransaction.commit();
}
 
Example 7
Source File: HomeBaseFragment.java    From kute with Apache License 2.0 6 votes vote down vote up
/************************ FUnctions to toggle fragment visibility ***************/
private void showHomeTab() {
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    if (home_tab.isAdded()) {
        fragmentTransaction.show(home_tab);
        Log.d(TAG, "Fragment transaction: HomeTab -> Found Previous One");
    } else {
        fragmentTransaction.add(R.id.frameBottomBar, home_tab, "HomeTab");
    }
    //Hide The other two fragments
    if (friend_tab.isAdded()) {
        fragmentTransaction.hide(friend_tab);
    }
    if (my_routes_tab.isAdded()) {
        fragmentTransaction.hide(my_routes_tab);
    }
    fragmentTransaction.commit();
}
 
Example 8
Source File: BaseRestoreFragment.java    From GalleryLayoutManager with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (BuildConfig.DEBUG) {
        Log.d(getClass().getSimpleName(), "onCreate:");
    }
    if (savedInstanceState != null && getFragmentManager() != null) {
        boolean isShow = savedInstanceState.getBoolean(IS_SHOW, true);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (BuildConfig.DEBUG) {
            Log.d(this.getClass().getSimpleName(), "restore show:" + isShow);
        }
        if (!isShow) {
            ft.hide(this);
        } else {
            ft.show(this);
        }
        ft.commit();
    }
}
 
Example 9
Source File: BaseStateFragment.java    From YCStateLayout with Apache License 2.0 6 votes vote down vote up
/**
 * 异常崩溃后会再次走onCreate方法,这也就是为啥有时候fragment重叠,因为被创建多次
 * 发生Fragment重叠的根本原因在于FragmentState没有保存Fragment的显示状态,
 * 即mHidden,导致页面重启后,该值为默认的false,即show状态,所以导致了Fragment的重叠。
 * 两种方案:第一种在activity中处理,第二种在fragment中处理
 * @param savedInstanceState                bundle
 */
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        //异常启动
        boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
        FragmentTransaction ft = null;
        if (getFragmentManager() != null) {
            ft = getFragmentManager().beginTransaction();
            if (isSupportHidden) {
                ft.hide(this);
            } else {
                ft.show(this);
            }
            ft.commit();
        }
    } else {
        //正常启动
    }
}
 
Example 10
Source File: MainActivity.java    From githot with Apache License 2.0 5 votes vote down vote up
private void selectFragment(int fragmentId) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    hideAllFragment(transaction);
    switch (fragmentId) {
        case R.id.nav_user_china:
            if (hotUsersFragment == null) {
                hotUsersFragment = new HotUsersMainFragment();
                // todo diff with transaction.replace() ?
                transaction.add(R.id.id_main_frame_container, hotUsersFragment, "hotUser");
            } else {
                transaction.show(hotUsersFragment);
            }
            ab.setTitle("Hot users");
            break;

        case R.id.nav_repositories:
            if (hotReposFragment == null) {
                hotReposFragment = new HotReposMainFragment();
                transaction.add(R.id.id_main_frame_container, hotReposFragment, "hotRepos");
            } else {
                transaction.show(hotReposFragment);
            }
            ab.setTitle("Hot repos");
            break;

        case R.id.nav_trending_repos:
            mTrendingSpinner.setVisibility(View.VISIBLE);
            if (trendingReposMainFragment == null) {
                trendingReposMainFragment = new TrendingReposMainFragment();
                transaction.add(R.id.id_main_frame_container, trendingReposMainFragment, "TrendingRepos");
            } else {
                transaction.show(trendingReposMainFragment);
            }
            ab.setTitle("");
            break;
    }
    transaction.commit();
}
 
Example 11
Source File: SearchMainActivity.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
private void changeFragment() {
    SearchWhat sw = getSearchWhat();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    if (sw == SearchWhat.status) {
        ft.show(getSearchStatusFragment());
        ft.hide(getSearchUserFragment());
    } else {
        ft.show(getSearchUserFragment());
        ft.hide(getSearchStatusFragment());
    }
    ft.commit();
}
 
Example 12
Source File: FragmentChangeManager.java    From likequanmintv with Apache License 2.0 5 votes vote down vote up
/** 界面切换控制 */
public void setFragments(int index) {
    for (int i = 0; i < mFragments.size(); i++) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        Fragment fragment = mFragments.get(i);
        if (i == index) {
            ft.show(fragment);
        } else {
            ft.hide(fragment);
        }
        ft.commit();
    }
    mCurrentTab = index;
}
 
Example 13
Source File: HotFragment.java    From BaoKanAndroid with MIT License 5 votes vote down vote up
@Override
protected void loadData() {
    mNewsListFragment = NewsListFragment.newInstance("461", false);
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (mNewsListFragment.isAdded()) {
        transaction.show(mNewsListFragment);
    } else {
        transaction.add(R.id.fl_hot_content, mNewsListFragment);
    }
    transaction.commit();
}
 
Example 14
Source File: SecondActivity.java    From YCShopDetailLayout with Apache License 2.0 5 votes vote down vote up
private void initShopMainFragment() {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    if(shopMainFragment==null){
        shopMainFragment = new ShopMain1Fragment();
        fragmentTransaction
                .replace(R.id.fl_shop_main2, shopMainFragment)
                .commit();
    }else {
        fragmentTransaction.show(shopMainFragment);
    }
}
 
Example 15
Source File: MainActivity.java    From scallop with MIT License 5 votes vote down vote up
/**
 * 根据给定下标选中对应的 fragment
 * @param index fragment 在列表中的下标
 */
public void selectFragment(int index) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    for (int i = 0; i < fragmentList.size(); i++) {
        if (i == index) {
            fragmentTransaction.show(fragmentList.get(i));
        } else {
            fragmentTransaction.hide(fragmentList.get(i));
        }
    }
    fragmentTransaction.commit();
}
 
Example 16
Source File: DetailVideoActivity.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 展示页面
 */
private void showPlayingFragment() {
    if (isPlayFragmentShow) {
        return;
    }

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
    if (mDetailAudioFragment == null) {
        mDetailAudioFragment = DetailAudioFragment.newInstance("Video");
        ft.add(android.R.id.content, mDetailAudioFragment);
    } else {
        ft.show(mDetailAudioFragment);
    }
    ft.commitAllowingStateLoss();
    isPlayFragmentShow = true;
    AppLogUtils.e("fragment数量+DetailVideoActivity" + FragmentUtils.getAllFragments(getSupportFragmentManager()).size());

    if (videoPlayer.isPlaying() || videoPlayer.isBufferingPlaying()) {
        videoPlayer.pause();
    }

    //当视频正在播放,准备播放时,点击音视频切换按钮,先暂停视频,然后记录视频播放位置,show音频播放页面
    //当视频已经暂停,播放错误,播放停止时,点击音视频切换按钮,直接记录视频播放位置,show音频播放页面
    BaseConfig.INSTANCE.setPosition(videoPlayer.getCurrentPosition());
    AppLogUtils.e("播放位置----视频页开始显示音频--" + videoPlayer.getCurrentPosition());

    if (mDetailAudioFragment != null) {
        mDetailAudioFragment.setViewData(BaseAppHelper.get().getMusicList().get(0));
        if (getPlayService().isDefault() || getPlayService().isPausing()) {
            getPlayService().seekTo((int) BaseConfig.INSTANCE.getPosition());
            getPlayService().playPause();
        }
    }
}
 
Example 17
Source File: MainActivity.java    From xmpp with Apache License 2.0 4 votes vote down vote up
/**
 * 点击不同的按钮做出不同的处理
 */
private void selection(int index) {
    initialImage();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment f : fragments) {
            ft.hide(f);
        }
    }

    Fragment fragment;
    switch (index) {

        case 0:
            iv_addfriend.setVisibility(View.GONE);
            tv_message.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.tab_move_pressed_icon, 0, 0);
            tv_message.setTextColor(tv_message.getResources().getColor(R.color.title));
            fragment = getSupportFragmentManager().findFragmentByTag("message_fragment");
            if (fragment == null) {
                message_fragment = new MessageFragment();
                ft.add(R.id.fg_content, message_fragment, "message_fragment");
            } else {
                ft.show(fragment);
            }
            break;
        case 1:
            iv_addfriend.setVisibility(View.VISIBLE);
            tv_friend.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.tab_me_pressed_icon, 0, 0);
            tv_friend.setTextColor(tv_friend.getResources().getColor(R.color.title));
            fragment = getSupportFragmentManager().findFragmentByTag("friend_fragment");
            if (fragment == null) {
                friend_fragment = new FriendFragment();
                ft.add(R.id.fg_content, friend_fragment, "friend_fragment");
            } else {
                ft.show(fragment);
            }
            break;

        case 2:
            iv_addfriend.setVisibility(View.GONE);
            tv_news.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.tab_comprehensive_pressed_icon, 0, 0);
            tv_news.setTextColor(tv_news.getResources().getColor(R.color.title));
            fragment = getSupportFragmentManager().findFragmentByTag("news_fragment");
            if (fragment == null) {
                news_fragment = new NewsFragment();
                ft.add(R.id.fg_content, news_fragment, "news_fragment");
            } else {
                ft.show(fragment);
            }
            break;
        case 3:
            iv_addfriend.setVisibility(View.GONE);
            tv_luntan.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.tab_found_pressed_icon, 0, 0);
            tv_luntan.setTextColor(tv_luntan.getResources().getColor(R.color.title));
            fragment = getSupportFragmentManager().findFragmentByTag("luntan_fragment");
            if (fragment == null) {
                luntan_fragment = new LuntanFragment();
                ft.add(R.id.fg_content, luntan_fragment, "luntan_fragment");
            } else {
                ft.show(fragment);
            }
            break;
    }
    ft.commit();
}
 
Example 18
Source File: BaseActivity.java    From JianDanRxJava with Apache License 2.0 4 votes vote down vote up
protected void showFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.show(fragment);
    transaction.commit();
}
 
Example 19
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();
}
 
Example 20
Source File: MainActivity.java    From GankGirl with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 设置默认的Fragment
 *
 * @param index 选项卡的标号:0, 1, 2, 3
 */
private void setDefaultFragment(int index) {
    //开启事务
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    hideFragments(fragmentTransaction);
    switch (index) {
        case 0:
            if (mGankTadPageFragment == null) {
                mGankTadPageFragment = GankTadPageFragment.newInstance(getString(R.string.button_navigation_home_text));
                fragmentTransaction.add(R.id.contentLayout, mGankTadPageFragment);
            } else {
                // 如果不为空,则直接将它显示出来
                fragmentTransaction.show(mGankTadPageFragment);
            }
            break;
        case 1:
            if (mReadTadPageFragment == null) {
                mReadTadPageFragment = ReadTadPageFragment.newInstance(getString(R.string.button_navigation_read_text));
                fragmentTransaction.add(R.id.contentLayout, mReadTadPageFragment);
            } else {
                // 如果不为空,则直接将它显示出来
                fragmentTransaction.show(mReadTadPageFragment);
            }
            break;
        case 2:
            if (mGirlFragment == null) {
                mGirlFragment = GirlFragment.newInstance(getString(R.string.button_navigation_girl_text));
                fragmentTransaction.add(R.id.contentLayout, mGirlFragment);
            } else {
                // 如果不为空,则直接将它显示出来
                fragmentTransaction.show(mGirlFragment);
            }
            break;
        case 3:
            if (mVideoFragment == null) {
                mVideoFragment = VideoFragment.newInstance(getString(R.string.button_navigation_video_text));
                fragmentTransaction.add(R.id.contentLayout, mVideoFragment);
            } else {
                // 如果不为空,则直接将它显示出来
                fragmentTransaction.show(mVideoFragment);
            }
            break;
    }
    fragmentTransaction.commit();   // 事务提交
}