Search in sources :

Example 86 with FragmentTransaction

use of android.support.v4.app.FragmentTransaction in project DesignLibrary by StylingAndroid.

the class ArticleViewPagerAdapter method destroyItem.

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    FragmentManager manager = ((Fragment) object).getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    trans.remove((Fragment) object);
    trans.commit();
    super.destroyItem(container, position, object);
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction) Fragment(android.support.v4.app.Fragment)

Example 87 with FragmentTransaction

use of android.support.v4.app.FragmentTransaction in project GT by Tencent.

the class GTMainActivity method setTabSelection.

/**
	 * 根据传入的index参数来设置选中的tab页。
	 * 
	 * @param index
	 *            每个tab页对应的下标。
	 */
private synchronized void setTabSelection(int index) {
    // 每次选中之前先清楚掉上次的选中状态
    clearSelection();
    // 开启一个Fragment事务
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
    //TODO
    hideFragments(transaction);
    switch(index) {
        case 0:
            // 当点击了AUT tab时,改变控件的图片和文字颜色
            autImage.setImageResource(R.drawable.tab_selected_border);
            autText.setTextColor(Color.WHITE);
            if (autFragment == null) {
                autFragment = new GTAUTFragment();
                transaction.add(R.id.content, autFragment, "a");
            } else {
                transaction.show(autFragment);
            }
            break;
        case 1:
            // 当点击了参数 tab时,改变控件的图片和文字颜色
            paramImage.setImageResource(R.drawable.tab_selected_border);
            paramText.setTextColor(Color.WHITE);
            if (paramFragment == null) {
                paramFragment = new GTParamTopFragment();
                transaction.add(R.id.content, paramFragment, "b");
            } else {
                transaction.show(paramFragment);
                paramFragment.onShow(true);
            }
            break;
        case 2:
            // 当点击了耗时 tab时,改变控件的图片和文字颜色
            perfImage.setImageResource(R.drawable.tab_selected_border);
            perfText.setTextColor(Color.WHITE);
            if (perfFragment == null) {
                perfFragment = new GTPerfFragment();
                transaction.add(R.id.content, perfFragment, "c");
            } else {
                transaction.show(perfFragment);
            }
            break;
        case 3:
            // 当点击了日志 tab时,改变控件的图片和文字颜色
            logImage.setImageResource(R.drawable.tab_selected_border);
            logText.setTextColor(Color.WHITE);
            if (logFragment == null) {
                logFragment = new GTLogFragment();
                transaction.add(R.id.content, logFragment, "d");
            } else {
                transaction.show(logFragment);
            }
            break;
        case 4:
        default:
            // 当点击了插件 tab时,改变控件的图片和文字颜色
            pluginImage.setImageResource(R.drawable.tab_selected_border);
            pluginText.setTextColor(Color.WHITE);
            if (pluginFragment == null) {
                pluginFragment = new GTPluginFragment();
                transaction.add(R.id.content, pluginFragment, "e");
            } else {
                transaction.show(pluginFragment);
            }
            break;
    }
    /*
		 * 直接使用commit()可能会出错:
		 * IllegalStateException: Can not perform this action after onSaveInstanceState:\
		 * 
		 * @see http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()
		 * 
		 * 大致意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState
		 * 方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就
		 * 是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。
		 */
    // transaction.commit(); 
    transaction.commitAllowingStateLoss();
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction)

Example 88 with FragmentTransaction

use of android.support.v4.app.FragmentTransaction in project GT by Tencent.

the class GTMainActivity method removeFragments.

/*
	 * 在Activity重新加载时,要先清除遗留的Fragment
	 */
private void removeFragments() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment fragment = fragmentManager.findFragmentByTag("a");
    if (fragment != null) {
        transaction.remove(fragment);
    }
    fragment = fragmentManager.findFragmentByTag("b");
    if (fragment != null) {
        transaction.remove(fragment);
    }
    fragment = fragmentManager.findFragmentByTag("c");
    if (fragment != null) {
        transaction.remove(fragment);
    }
    fragment = fragmentManager.findFragmentByTag("d");
    if (fragment != null) {
        transaction.remove(fragment);
    }
    fragment = fragmentManager.findFragmentByTag("e");
    if (fragment != null) {
        transaction.remove(fragment);
    }
    transaction.commitAllowingStateLoss();
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction) Fragment(android.support.v4.app.Fragment)

Example 89 with FragmentTransaction

use of android.support.v4.app.FragmentTransaction in project GT by Tencent.

the class GTParamTopFragment method setFragSelection.

/**
	 * 根据传入的index参数来设置选中的Fragment页。
	 * 
	 * @param index
	 *            每个Fragment页对应的下标。
	 */
private void setFragSelection(int index) {
    // 每次选中之前先清楚掉上次的选中状态
    clearSelection();
    // 开启一个Fragment事务
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
    hideFragments(transaction);
    switch(index) {
        case 0:
            // 入参
            btnIn.setBackgroundResource(R.drawable.swbtn_selected);
            btnIn.setTextColor(Color.WHITE);
            if (inFragment == null) {
                inFragment = new GTParamInFragment();
                transaction.add(R.id.content, inFragment);
            } else {
                transaction.show(inFragment);
            }
            break;
        case 1:
            // 出参
            btnOut.setBackgroundResource(R.drawable.swbtn_selected);
            btnOut.setTextColor(Color.WHITE);
            if (outFragment == null) {
                outFragment = new GTParamOutFragment();
                transaction.add(R.id.content, outFragment);
            } else {
                transaction.show(outFragment);
            }
            break;
        case 2:
            // 入参编辑
            btnIn.setBackgroundResource(R.drawable.swbtn_selected);
            btnIn.setTextColor(Color.WHITE);
            if (inEditFragment == null) {
                inEditFragment = new GTParamInEditFragment();
                transaction.add(R.id.content, inEditFragment);
            } else {
                transaction.show(inEditFragment);
            }
            break;
        case 3:
            // 出参编辑
            btnOut.setBackgroundResource(R.drawable.swbtn_selected);
            btnOut.setTextColor(Color.WHITE);
            if (outEditFragment == null) {
                outEditFragment = new GTParamOutEditFragment();
                transaction.add(R.id.content, outEditFragment);
            } else {
                transaction.show(outEditFragment);
            }
            break;
        default:
            break;
    }
    /*
		 * 直接使用commit()可能会出错:
		 * IllegalStateException: Can not perform this action after onSaveInstanceState:\
		 * 
		 * @see http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()
		 * 
		 * 大致意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState
		 * 方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就
		 * 是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。
		 */
    // transaction.commit(); 
    transaction.commitAllowingStateLoss();
}
Also used : FragmentTransaction(android.support.v4.app.FragmentTransaction)

Example 90 with FragmentTransaction

use of android.support.v4.app.FragmentTransaction in project mounts2sd by SpazeDog.

the class ActivityTabController method tabSwitchFragment.

private void tabSwitchFragment(Integer tabId) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Boolean isAdded = fragmentManager.findFragmentByTag("" + tabId) != null;
    if (!isAdded || mTabFragments.get(tabId).isDetached()) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        for (int id : mTabFragments.keySet()) {
            if (id != tabId && fragmentManager.findFragmentByTag("" + id) != null && !mTabFragments.get(id).isDetached()) {
                fragmentTransaction.detach(mTabFragments.get(id));
            }
        }
        if (isAdded && mTabFragments.get(tabId).isDetached()) {
            fragmentTransaction.attach(mTabFragments.get(tabId));
        } else if (!isAdded) {
            fragmentTransaction.add(R.id.fragment_frame, mTabFragments.get(tabId), "" + tabId);
        }
        fragmentTransaction.commit();
    } else {
        tabUpdateButtons();
    }
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction)

Aggregations

FragmentTransaction (android.support.v4.app.FragmentTransaction)392 Fragment (android.support.v4.app.Fragment)135 FragmentManager (android.support.v4.app.FragmentManager)79 View (android.view.View)29 DialogFragment (android.support.v4.app.DialogFragment)27 FragmentActivity (android.support.v4.app.FragmentActivity)25 Bundle (android.os.Bundle)21 TextView (android.widget.TextView)19 FragmentTransaction (android.app.FragmentTransaction)18 Intent (android.content.Intent)18 Button (android.widget.Button)17 OnClickListener (android.view.View.OnClickListener)16 SherlockFragment (com.actionbarsherlock.app.SherlockFragment)12 ActionBar (android.support.v7.app.ActionBar)8 TargetApi (android.annotation.TargetApi)6 AppCompatActivity (android.support.v7.app.AppCompatActivity)6 Toolbar (android.support.v7.widget.Toolbar)6 FriendsTimeLineFragment (org.qii.weiciyuan.ui.maintimeline.FriendsTimeLineFragment)6 UserInfoFragment (org.qii.weiciyuan.ui.userinfo.UserInfoFragment)6 Transition (android.transition.Transition)5