下面列出了android.support.v4.app.FragmentManager#beginTransaction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(17)
private void showAlipayDialog(boolean notOpenFlag) {
FragmentManager fm = getSupportFragmentManager();
if (fm != null && getActivity() != null && !getActivity().isDestroyed()) {
FragmentTransaction ft = fm.beginTransaction();
this.mAlipayAutoPayDialog = (AlipayAutoPayDialog) fm.findFragmentByTag("showmAlipayPayDialog");
Bundle bundle = new Bundle();
bundle.putBoolean(AlipayConstant.NOT_OPEN_CONTINUE_MONTHLY, notOpenFlag);
bundle.putBoolean(AlipayConstant.IS_MOBILE_VIP_FLAG, this.mIsMobileVipFlag);
if (this.mAlipayAutoPayDialog == null) {
this.mAlipayAutoPayDialog = new AlipayAutoPayDialog();
} else {
ft.remove(this.mAlipayAutoPayDialog);
}
this.mAlipayAutoPayDialog.setAlipayConfirmCallback(this);
if (this.mAlipayAutoPayDialog.getArguments() == null) {
this.mAlipayAutoPayDialog.setArguments(bundle);
}
ft.add(this.mAlipayAutoPayDialog, "showmAlipayPayDialog");
ft.commitAllowingStateLoss();
}
}
public void onJsShareDialogShow(String jsonString) {
FragmentManager fm = getSupportFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
if (this.mInviteShareProtocol == null) {
LeResponseMessage response = LeMessageManager.getInstance().dispatchMessage(new LeMessage(LeMessageIds.MSG_SHARE_INVITE_WEBVIEW_INIT));
if (LeResponseMessage.checkResponseMessageValidity(response, WebViewInviteShareProtocol.class)) {
this.mInviteShareProtocol = (WebViewInviteShareProtocol) response.getData();
}
} else {
ft.remove(this.mInviteShareProtocol.getFragment());
}
if (this.mInviteShareProtocol != null) {
this.mInviteShareProtocol.setShareText(jsonString);
ft.add(this.mInviteShareProtocol.getFragment(), "shareDialogs");
ft.commitAllowingStateLoss();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
ImagesFragment imagesFragment =
(ImagesFragment) getSupportFragmentManager().findFragmentById(R.id.contentframe);
if (imagesFragment == null) {
imagesFragment = new ImagesFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.contentframe, imagesFragment);
transaction.commit();
}
mView = imagesFragment;
mPresenter = new ImagesPresenter(Injection.getImageRepository(this), mView);
}
/**
* Add first fragment to the activity. This fragment will be attached to the bottom of the fragments stack.
* When fragment stack is cleared {@link #clearBackStack}, this fragment will be shown.
*/
private void addInitialFragment() {
Fragment fragment = new BannersFragment();
FragmentManager frgManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = frgManager.beginTransaction();
fragmentTransaction.add(R.id.main_content_frame, fragment).commit();
frgManager.executePendingTransactions();
}
/**
*
*/
public static void replaceFragmentToActivity(@NonNull FragmentManager fragmentManager,
@NonNull Fragment fragment, int frameId) {
checkNotNull(fragmentManager, "");
checkNotNull(fragment, "");
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(frameId, fragment);
transaction.commit();
}
public static void showIntentChooserDialog(@NonNull FragmentManager fm, int type) {
FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(TAG);
if (prev != null) {
ft.remove(prev);
}
try {
DialogFragment dialog = IntentChooserFragment.newInstance(type);
dialog.show(ft, TAG);
} catch (IllegalArgumentException | IllegalStateException ignored) {}
}
@Override
public void onDestroyView() {
FragmentManager fm = getChildFragmentManager();
SupportMapFragment map = (SupportMapFragment) fm.findFragmentById(R.id.map);
FragmentTransaction ft = fm.beginTransaction();
ft.remove(map);
ft.commitAllowingStateLoss();
super.onDestroyView();
}
private void setDefaultFragment(){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.tab_content_container, new CreateMailFragment());
fragmentTransaction.commit();
}
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();
}
private static void replace(AppCompatActivity activity,
Fragment fragment,
@IdRes int containerId,
boolean backStack) {
if (activity.isFinishing()) return;
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
setCustomAnimations(transaction);
if (backStack) transaction.addToBackStack(null);
transaction.replace(containerId, fragment).commit();
}
/**
* 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) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(frameId, fragment);
transaction.commit();
}
public static void load(final FragmentManager fragmentManager, final Boolean toBackStack) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new MainActivityFragment());
if (toBackStack) {
fragmentTransaction.addToBackStack(FRAGMENT_NAME);
}
fragmentTransaction.commit();
}
private static void showDialog(@NonNull AppCompatActivity activity,
@NonNull DialogFragment fragment,
@NonNull String tag) {
Check.getInstance().isInMainThread();
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(tag);
if (prev != null) ft.remove(prev);
ft.addToBackStack(null);
fragment.show(ft, tag);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_earthquake_main);
FragmentManager fm = getSupportFragmentManager();
// Android will automatically re-add any Fragments that
// have previously been added after a configuration change,
// so only add it if this isn't an automatic restart.
if (savedInstanceState == null) {
FragmentTransaction ft = fm.beginTransaction();
mEarthquakeListFragment = new EarthquakeListFragment();
ft.add(R.id.main_activity_frame,
mEarthquakeListFragment, TAG_LIST_FRAGMENT);
ft.commitNow();
} else {
mEarthquakeListFragment =
(EarthquakeListFragment)fm.findFragmentByTag(TAG_LIST_FRAGMENT);
}
Date now = Calendar.getInstance().getTime();
List<Earthquake> dummyQuakes = new ArrayList<Earthquake>(0);
dummyQuakes.add(new Earthquake("0", now, "San Jose", null, 7.3, null));
dummyQuakes.add(new Earthquake("1", now, "LA", null, 6.5, null));
mEarthquakeListFragment.setEarthquakes(dummyQuakes);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
FragmentManager fm = getChildFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
mFragment3 = (Menu3Fragment)fm.findFragmentByTag("f3");
if (mFragment3 == null) {
mFragment3 = new Menu3Fragment();
ft.add(mFragment3, "f3");
}
ft.commit();
}
public static void showOtherAppsDialog(@NonNull FragmentManager fm) {
FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(TAG);
if (prev != null) {
ft.remove(prev);
}
try {
DialogFragment dialog = OtherAppsFragment.newInstance();
dialog.show(ft, TAG);
} catch (IllegalStateException | IllegalArgumentException ignored) {}
}
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);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_earthquake_main);
FragmentManager fm = getSupportFragmentManager();
// Android will automatically re-add any Fragments that
// have previously been added after a configuration change,
// so only add it if this isn't an automatic restart.
if (savedInstanceState == null) {
FragmentTransaction ft = fm.beginTransaction();
mEarthquakeListFragment = new EarthquakeListFragment();
ft.add(R.id.main_activity_frame,
mEarthquakeListFragment, TAG_LIST_FRAGMENT);
ft.commitNow();
} else {
mEarthquakeListFragment =
(EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
}
// Retrieve the Earthquake View Model for this Activity.
earthquakeViewModel = ViewModelProviders.of(this)
.get(EarthquakeViewModel.class);
}
protected void addFragment(FragmentManager fragmentManager, int containerId, Fragment fragment) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(containerId, fragment);
fragmentTransaction.commit();
}
public static SupportFragmentTransactionBuilder transaction(FragmentManager fragmentManager) {
return new SupportFragmentTransactionBuilder(fragmentManager,
fragmentManager.beginTransaction());
}