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

The following examples show how to use android.support.v4.app.FragmentTransaction#add() . 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: TestFragmentV4Activity.java    From HelloTransactionAnimations with Apache License 2.0 6 votes vote down vote up
private void addFragment() {
	if (null == mFragmentManager) {
		mFragmentManager = getSupportFragmentManager();
	}

	mTextFragmentOne = new MyFragmentOne();
	FragmentTransaction fragmentTransaction = mFragmentManager
			.beginTransaction();
	fragmentTransaction.setCustomAnimations(
			R.anim.push_left_in,
			R.anim.push_left_out,
			R.anim.push_left_in,
			R.anim.push_left_out);

	fragmentTransaction.add(R.id.container, mTextFragmentOne);

	fragmentTransaction.addToBackStack(null);
	fragmentTransaction.commit();
}
 
Example 2
Source File: TaxActivityHandler.java    From mobikul-standalone-pos with MIT License 6 votes vote down vote up
public void addTax() {
    FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
    Fragment fragment;
    fragment = ((BaseActivity) context).mSupportFragmentManager.findFragmentByTag(AddTaxFragment.class.getSimpleName());
    if (fragment == null)
        fragment = new AddTaxFragment();
    if (!fragment.isAdded()) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("tax", new Tax());
        bundle.putBoolean("edit", false);
        fragment.setArguments(bundle);
        fragmentTransaction.add(((TaxActivity) context).binding.taxFl.getId(), fragment, fragment.getClass().getSimpleName());
        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName()).commit();
    }
}
 
Example 3
Source File: ViewPagerFragmentAdapter.java    From iBeebo with GNU General Public License v3.0 6 votes vote down vote up
public ViewPagerFragmentAdapter(Fragment fragment, ViewPager viewPager, FragmentManager fm, ArrayList<ChildPage> fragmentList) {
    super(fm);
    this.mFragmentList = fragmentList;

    FragmentTransaction transaction = fragment.getChildFragmentManager().beginTransaction();

    for (int i = 0; i < fragmentList.size(); i++) {
        Fragment ft = this.mFragmentList.get(i).getmFragment();
        if (!ft.isAdded()) {
            transaction.add(viewPager.getId(), ft, ft.getClass().getName() + i);
        }
    }

    if (!transaction.isEmpty()) {
        transaction.commit();
        fragment.getChildFragmentManager().executePendingTransactions();
    }
}
 
Example 4
Source File: SocialMainActivity.java    From Social with Apache License 2.0 6 votes vote down vote up
private void clickNearby(FragmentTransaction fragmentTransaction) {
    position = 0;
    resetBottomItemSelected();
    mTvNearby.setSelected(true);
    if (mainFragment == null) {
        mainFragment = new MainFragment();
        if (saveInstanceState ==null) fragmentTransaction.add(R.id.ly_content, mainFragment,"mainFragment");
        else {
            mainFragment = (MainFragment)fManager.findFragmentByTag("mainFragment");
            if(mainFragment==null){
                mainFragment = new MainFragment();
                fragmentTransaction.add(R.id.ly_content, mainFragment,"mainFragment");
            }else{
                fragmentTransaction.show(mainFragment);
            }
        }
    } else {
        fragmentTransaction.show(mainFragment);
    }
    fragmentTransaction.commit();
}
 
Example 5
Source File: FragmentContainerActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Inflate the layout containing the Fragment containers
  setContentView(R.layout.fragment_container_layout);

  FragmentManager fragmentManager = getSupportFragmentManager();

  // Check to see if the Fragment containers have been populated
  // with Fragment instances. If not, create and populate the layout.
  DetailFragment detailsFragment =
    (DetailFragment) fragmentManager.findFragmentById(R.id.details_container);

  if (detailsFragment == null) {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.add(R.id.details_container, new DetailFragment());
    ft.add(R.id.list_container, new MyListFragment());
    ft.commitNow();
  }
}
 
Example 6
Source File: MainActivity.java    From sms-ticket with Apache License 2.0 6 votes vote down vote up
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    mSelectedTab = tab.getPosition();
    String selectedTag = (mSelectedTab == TAB_TICKETS) ? TAG_TICKETS : TAG_STATS;
    ProjectBaseFragment preInitializedFragment = (ProjectBaseFragment)getSupportFragmentManager()
        .findFragmentByTag(selectedTag);
    if (preInitializedFragment == null) {
        switch (mSelectedTab) {
            case TAB_TICKETS:
                mCurrentFragment = TicketsFragment.newInstance(getIntent().getLongExtra(EXTRA_TICKET_ID,
                    TicketsFragment.NONE), getIntent().getBooleanExtra(EXTRA_SHOW_SMS, false));
                break;
            case TAB_STATISTICS:
                mCurrentFragment = new StatisticsFragment();
                break;
        }
        ft.add(android.R.id.content, mCurrentFragment, selectedTag);
    } else {
        mCurrentFragment = preInitializedFragment;
        ft.attach(preInitializedFragment);
    }
}
 
Example 7
Source File: MainActivity.java    From admob-native-advanced-feed with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize the Mobile Ads SDK.
    MobileAds.initialize(this, getString(R.string.admob_app_id));

    if (savedInstanceState == null) {
        // Create new fragment to display a progress spinner while the data set for the
        // RecyclerView is populated.
        Fragment loadingScreenFragment = new LoadingScreenFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, loadingScreenFragment);

        // Commit the transaction.
        transaction.commit();

        // Update the RecyclerView item's list with menu items.
        addMenuItemsFromJson();
        // Update the RecyclerView item's list with native ads.
        loadNativeAds();
    }
}
 
Example 8
Source File: TabPagerAdapter.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = fragments.get(position);
    if(!fragment.isAdded()){ // 如果fragment还没有added
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(fragment, "tab_" + position);
        ft.commit();
        /**
         * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
         * 会在进程的主线程中,用异步的方式来执行。
         * 如果想要立即执行这个等待中的操作,就要调用这个方法(只能在主线程中调用)。
         * 要注意的是,所有的回调和相关的行为都会在这个调用中被执行完成,因此要仔细确认这个方法的调用位置。
         */
        fragmentManager.executePendingTransactions();
    }

    if(fragment.getView().getParent() == null){
        container.addView(fragment.getView()); // 为viewpager增加布局
    }

    return fragment.getView();
}
 
Example 9
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 10
Source File: NoteDetailActivity.java    From androidtestdebug with MIT License 5 votes vote down vote up
private void initFragment(Fragment detailFragment) {
    // Add the NotesDetailFragment to the layout
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(R.id.contentFrame, detailFragment);
    transaction.commit();
}
 
Example 11
Source File: ActivityUtils.java    From Stock-Hawk with Apache License 2.0 5 votes vote down vote up
/**
 * The {@code fragment} is added to the container view with movieId {@code frameId}. The operation is
 * performed by the {@code fragmentManager}.
 *
 */
public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
                                          @NonNull Fragment fragment, int frameId) {
    checkNotNull(fragmentManager);
    checkNotNull(fragment);
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(frameId, fragment);
    transaction.commit();
}
 
Example 12
Source File: DoublelistlinkageActivity.java    From AndroidSamples with Apache License 2.0 5 votes vote down vote up
/**
 * 创建右侧列表
 */
public void createFragment() {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    mSortDetailFragment = new SortDetailFragment();
    mSortDetailFragment.setListener(this);
    fragmentTransaction.add(R.id.lin_fragment, mSortDetailFragment);
    fragmentTransaction.commit();
}
 
Example 13
Source File: BaseFragmentStyleActivity.java    From SimpleProject with MIT License 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_container_layout);

	Fragment fragment = BaseFragmentStyleFragment.getInstance(getIntent().getIntExtra("toolbarStyle", 0));
	FragmentManager fm = getSupportFragmentManager();
	FragmentTransaction transaction = fm.beginTransaction();
	transaction.add(R.id.container_layout, fragment);
	transaction.commit();
}
 
Example 14
Source File: BottomSheetFragmentDelegate.java    From ThreePhasesBottomSheet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * DialogFragment-like show() method for displaying this the associated sheet fragment
 *
 * @param transaction FragmentTransaction instance
 * @param bottomSheetLayoutId Resource ID of the {@link BottomSheetLayout}
 * @return the back stack ID of the fragment after the transaction is committed.
 */
public int show(FragmentTransaction transaction, @IdRes int bottomSheetLayoutId) {
    dismissed = false;
    shownByMe = true;
    this.bottomSheetLayoutId = bottomSheetLayoutId;
    transaction.add(fragment, String.valueOf(bottomSheetLayoutId));
    viewDestroyed = false;
    backStackId = transaction.commit();
    return backStackId;
}
 
Example 15
Source File: ActivityUtils.java    From DoingDaily with Apache License 2.0 5 votes vote down vote up
/**
 * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
 * performed by the {@code fragmentManager}.
 */
public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                         @NonNull Fragment fragment, int frameId) {
    if (fragmentManager != null && fragment != null) {

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }
}
 
Example 16
Source File: ActivityUtil.java    From ClassSchedule with Apache License 2.0 5 votes vote down vote up
public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId) {
    checkNotNull(fragmentManager, "");
    checkNotNull(fragment, "");
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(frameId, fragment);
    transaction.commit();
}
 
Example 17
Source File: DoubleChatRoomActivity.java    From sealtalk-android with MIT License 5 votes vote down vote up
private void enterFragment1(Conversation.ConversationType mConversationType, String mTargetId) {

        ConversationFragment fragment = new ConversationFragment();

        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
                .appendPath("conversation").appendPath(mConversationType.getName().toLowerCase())
                .appendQueryParameter("targetId", mTargetId).build();

        fragment.setUri(uri);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.temp1, fragment);
        transaction.commit();
    }
 
Example 18
Source File: MainActivity.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void selectedFragment(int position) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    hideFragment(transaction);
    switch (position) {
        case 0:
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                transaction.add(R.id.content, homeFragment);
            } else {
                transaction.show(homeFragment);
            }
            break;
        case 1:
            if (friendsListFragment == null) {
                friendsListFragment = new FriendsListFragment();
                transaction.add(R.id.content, friendsListFragment);
            } else {
                transaction.show(friendsListFragment);
            }
            break;
        case 2:
            if (newsFragment == null) {
                newsFragment = new NewsFragment();
                transaction.add(R.id.content, newsFragment);
            } else {
                transaction.show(newsFragment);
            }
            break;
        case 3:
            if (applicationFragment == null) {
                applicationFragment = new DappFragment();
                transaction.add(R.id.content, applicationFragment);
            } else {
                transaction.show(applicationFragment);
            }
            break;
    }
    transaction.commit();
}
 
Example 19
Source File: MovieCollection.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
	MizuuApplication.setupTheme(this);
	
	Fragment frag = getSupportFragmentManager().findFragmentByTag(TAG);
	if (frag == null) {
		final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.add(R.id.content, CollectionLibraryFragment.newInstance(getIntent().getExtras().getString("collectionId"), getIntent().getExtras().getString("collectionTitle")), TAG);
		ft.commit();
	}
	
	setTitle(getIntent().getExtras().getString("collectionTitle"));
}
 
Example 20
Source File: BaseActivity.java    From News with Apache License 2.0 4 votes vote down vote up
protected void addFragment(FragmentManager fragmentManager, int containerId, Fragment fragment) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(containerId, fragment);
    fragmentTransaction.commit();
}