下面列出了android.app.FragmentManager#executePendingTransactions ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Builds Fragment and adds it to view hierarchy
* @return returns YoutubeOverlayFragment instance that was added to hierarchy
*/
public YoutubeOverlayFragment buildAndAdd() {
Preconditions.checkNotNull(activity);
Preconditions.checkArgument(!Strings.isNullOrEmpty(ytKey));
Preconditions.checkArgument(scrollableViewId != 0);
FragmentManager fragmentManager = activity.getFragmentManager();
View scrollableView = activity.findViewById(scrollableViewId);
YoutubeOverlayFragment yt = (YoutubeOverlayFragment) fragmentManager.findFragmentByTag(YoutubeOverlayFragment.class.getName());
if (yt != null) {
fragmentManager.beginTransaction().remove(yt).commit();
fragmentManager.executePendingTransactions();
}
yt = new YoutubeOverlayFragment();
Bundle bundle = new Bundle();
bundle.putIntArray(YoutubeOverlayFragment.HIDEABLE_VIEWS, Ints.toArray(hideableViews));
bundle.putString(YoutubeOverlayFragment.YT_DEVELOPER_KEY, ytKey);
bundle.putInt(SCROLLABLE_VIEW_ID, scrollableViewId);
yt.setArguments(bundle);
fragmentManager.beginTransaction().add(((View) scrollableView.getParent()).getId(), yt, YoutubeOverlayFragment.class.getName()).commit();
return yt;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void startFragment(@Nonnull Fragment fragment, @Nullable Class activityClass) {
if (activityClass == null) {
activityClass = Activity.class;
}
Activity activity = Robolectric.buildActivity(activityClass)
.create()
.start()
.resume()
.get();
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main, fragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void startFragment(@NonNull final Fragment fragment, @Nullable Class activityClass) {
final Activity activity = (Activity) Robolectric
.buildActivity(activityClass == null ? Activity.class : activityClass)
.create()
.start()
.resume()
.get();
final FragmentManager fragmentManager = activity.getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main, fragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
}
public static void commitFragment(@NonNull FragmentManager fragmentManager, @NonNull FragmentTransaction transaction, boolean commitNow) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (commitNow) {
transaction.commitNowAllowingStateLoss();
} else {
transaction.commitAllowingStateLoss();
}
} else {
transaction.commitAllowingStateLoss();
if (commitNow) {
fragmentManager.executePendingTransactions();
}
}
}
private WindowCaptureFragment getWindowCaptureFragment(@NonNull Activity activity) {
WindowCaptureFragment windowCaptureFragment = findScreenCaptureFragment(activity);
boolean isNewInstance = windowCaptureFragment == null;
if (isNewInstance) {
windowCaptureFragment = new WindowCaptureFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager.beginTransaction().add(windowCaptureFragment, TAG).commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return windowCaptureFragment;
}
private void changeFragment(Fragment newFragment, FragmentsAvailable newFragmentType, boolean withoutAnimation) {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
/*if (!withoutAnimation && !isAnimationDisabled && currentFragment.shouldAnimate()) {
if (newFragmentType.isRightOf(currentFragment)) {
transaction.setCustomAnimations(R.anim.slide_in_right_to_left,
R.anim.slide_out_right_to_left,
R.anim.slide_in_left_to_right,
R.anim.slide_out_left_to_right);
} else {
transaction.setCustomAnimations(R.anim.slide_in_left_to_right,
R.anim.slide_out_left_to_right,
R.anim.slide_in_right_to_left,
R.anim.slide_out_right_to_left);
}
}*/
if (newFragmentType != FragmentsAvailable.DIALER
&& newFragmentType != FragmentsAvailable.CONTACTS_LIST
&& newFragmentType != FragmentsAvailable.CHAT_LIST
&& newFragmentType != FragmentsAvailable.HISTORY_LIST) {
transaction.addToBackStack(newFragmentType.toString());
} else {
while (fm.getBackStackEntryCount() > 0) {
fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
transaction.replace(R.id.fragmentContainer, newFragment, newFragmentType.toString());
transaction.commitAllowingStateLoss();
fm.executePendingTransactions();
currentFragment = newFragmentType;
}
private AvoidOnResultFragment getAvoidOnResultFragment(Activity activity) {
AvoidOnResultFragment avoidOnResultFragment = findAvoidOnResultFragment(activity);
if (avoidOnResultFragment == null) {
avoidOnResultFragment = new AvoidOnResultFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager
.beginTransaction()
.add(avoidOnResultFragment, TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return avoidOnResultFragment;
}
private RxPermissionsFragment getRxPermissionsFragment(Activity activity) {
RxPermissionsFragment rxPermissionsFragment = findRxPermissionsFragment(activity);
boolean isNewInstance = rxPermissionsFragment == null;
if (isNewInstance) {
rxPermissionsFragment = new RxPermissionsFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager
.beginTransaction()
.add(rxPermissionsFragment, TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return rxPermissionsFragment;
}
private RxPermissionsFragment getRxPermissionsFragment(Activity activity) {
RxPermissionsFragment rxPermissionsFragment = findRxPermissionsFragment(activity);
boolean isNewInstance = rxPermissionsFragment == null;
if (isNewInstance) {
rxPermissionsFragment = new RxPermissionsFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager
.beginTransaction()
.add(rxPermissionsFragment, TAG)
.commit();
fragmentManager.executePendingTransactions();
}
return rxPermissionsFragment;
}
private OnActResultEventDispatcherFragment getEventDispatchFragment(Activity activity) {
final FragmentManager fragmentManager = activity.getFragmentManager();
OnActResultEventDispatcherFragment fragment = findEventDispatchFragment(fragmentManager);
if (fragment == null) {
fragment = new OnActResultEventDispatcherFragment();
fragmentManager
.beginTransaction()
.add(fragment, OnActResultEventDispatcherFragment.TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return fragment;
}
public void show(@NonNull final FragmentManager manager) {
FragmentTransaction transaction = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag(DIALOG_TAG);
if (prev != null) {
transaction.remove(prev);
}
transaction.add(this, DIALOG_TAG);
transaction.commitAllowingStateLoss();
manager.executePendingTransactions();
}
private void b()
{
FragmentManager fragmentmanager = getFragmentManager();
FragmentTransaction fragmenttransaction = fragmentmanager.beginTransaction();
a = (DynamicFragment)Fragment.instantiate(this, cn/com/smartdevices/bracelet/ui/DynamicFragment.getName());
fragmenttransaction.add(0x7f0a01bf, a, "DynamicFragment");
fragmenttransaction.commit();
fragmentmanager.executePendingTransactions();
p();
r();
I = LuaEvent.getInstance(this);
c();
aa = findViewById(0x7f0a01b9);
k = findViewById(0x7f0a01ba);
}
public void topFragment(Class<?> cls, int transition, Bundle args) {
Fragment frag = Fragment.instantiate(
this,
cls.getName(),
args
);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction()
.replace(R.id.content_frame, frag)
.setTransition(transition)
.commit();
fragmentManager.executePendingTransactions();
configureFragment(frag);
}
public void switchFragment(Class<?> cls, String name, Bundle args) {
Fragment frag = Fragment.instantiate(
this,
cls.getName(),
args
);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, frag)
.addToBackStack(name)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
fragmentManager.executePendingTransactions();
configureFragment(frag);
}
protected void changeFragment(Fragment fragment, String name, boolean isChild) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (transaction.isAddToBackStackAllowed()) {
int count = fragmentManager.getBackStackEntryCount();
if (count > 0) {
FragmentManager.BackStackEntry entry =
fragmentManager.getBackStackEntryAt(count - 1);
if (entry != null && name.equals(entry.getName())) {
fragmentManager.popBackStack();
if (!isChild) {
// We just removed it's duplicate from the back stack
// And we want at least one in it
transaction.addToBackStack(name);
}
}
}
if (isChild) {
transaction.addToBackStack(name);
}
}
if (getResources().getBoolean(R.bool.hide_bottom_bar_on_second_level_views)) {
if (isChild) {
if (!isTablet()) {
hideTabBar();
}
} else {
showTabBar();
}
}
Compatibility.setFragmentTransactionReorderingAllowed(transaction, false);
if (isChild && isTablet()) {
transaction.replace(R.id.fragmentContainer2, fragment, name);
findViewById(R.id.fragmentContainer2).setVisibility(View.VISIBLE);
} else {
transaction.replace(R.id.fragmentContainer, fragment, name);
}
transaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
private void showDetailFragment(int containerId, String rssItemUrl, boolean addToBackStack) {
// find detail fragment first
FragmentManager fm = getFragmentManager();
RssfeedDetailFragment detailFragment =
(RssfeedDetailFragment) fm.findFragmentByTag(TAG_DETAIL);
if (detailFragment == null) {
// create new detail fragment
detailFragment = RssfeedDetailFragment.instantiate(rssItemUrl);
// add fragment to the layout
addDetailFragment(detailFragment, containerId, addToBackStack, fm);
} else if (detailFragment.getId() != containerId) {
// remove fragment from old container
fm.beginTransaction().remove(detailFragment).commit();
fm.executePendingTransactions();
// add fragment to the new container
addDetailFragment(detailFragment, containerId, addToBackStack, fm);
} // else, fragment is already at the right place
detailFragment.setUrl(rssItemUrl);
}