下面列出了android.app.FragmentTransaction#commitAllowingStateLoss ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Add search fragment. Note this is called during onLayout, so there's some restrictions,
* such as executePendingTransaction can't be used in it.
*/
private void addSearchFragment() {
// In order to take full advantage of "fragment deferred start", we need to create the
// search fragment after all other fragments are created.
// The other fragments are created by the ViewPager on the first onMeasure().
// We use the first onLayout call, which is after onMeasure().
// Just return if the fragment is already created, which happens after configuration
// changes.
if (mSearchFragment != null) return;
final FragmentTransaction ft = getFragmentManager().beginTransaction();
final Fragment searchFragment = new PhoneNumberPickerFragment();
searchFragment.setUserVisibleHint(false);
ft.add(R.id.dialtacts_frame, searchFragment);
ft.hide(searchFragment);
ft.commitAllowingStateLoss();
}
public static void show(FragmentManager fm, Intent includeApps) {
final Bundle args = new Bundle();
args.putParcelable(EXTRA_INCLUDE_APPS, includeApps);
final RootsFragment fragment = new RootsFragment();
fragment.setArguments(args);
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_roots, fragment);
ft.commitAllowingStateLoss();
}
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();
}
}
}
/**
* トークンデータの詳細を表示する.
* @param token トークンデータ
*/
private void openAccessTokenDescription(final SQLiteToken token) {
AccessTokenDescriptionFragment f = new AccessTokenDescriptionFragment();
Bundle bundle = new Bundle();
bundle.putString(AccessTokenDescriptionFragment.EXTRA_CLIENT_ID,
token.getClientId());
f.setArguments(bundle);
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction t = fm.beginTransaction();
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
t.add(android.R.id.content, f, "description");
t.addToBackStack("tokenList");
t.commitAllowingStateLoss();
}
public static void show(FragmentManager fm) {
final PickFragment fragment = new PickFragment();
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_save, fragment, TAG);
ft.commitAllowingStateLoss();
}
@Override
public boolean startFragment(@NonNull UriRequest request, @NonNull Bundle bundle) throws ActivityNotFoundException, SecurityException {
String fragmentClassName = request.getStringField(FragmentTransactionHandler.FRAGMENT_CLASS_NAME);
if (TextUtils.isEmpty(fragmentClassName)) {
Debugger.fatal("FragmentTransactionHandler.handleInternal()应返回的带有ClassName");
return false;
}
if (mContainerViewId == 0) {
Debugger.fatal("FragmentTransactionHandler.handleInternal()mContainerViewId");
return false;
}
try {
Fragment fragment = Fragment.instantiate(request.getContext(), fragmentClassName, bundle);
if (fragment == null) {
return false;
}
FragmentTransaction transaction = mFragmentManager.beginTransaction();
switch (mStartType) {
case TYPE_ADD:
transaction.add(mContainerViewId, fragment, mTag);
break;
case TYPE_REPLACE:
transaction.replace(mContainerViewId, fragment, mTag);
break;
}
if (mAllowingStateLoss) {
transaction.commitAllowingStateLoss();
} else {
transaction.commit();
}
return true;
} catch (Exception e) {
Debugger.e(e);
return false;
}
}
/**清空所有除了带此tag的fragment*/
public void removeAllExcept(String tag){
FragmentTransaction ft = fm.beginTransaction();
for (Fragment f:fs) {
if(f.getTag()!=null&&!f.getTag().equals(tag))ft.remove(f);
}
ft.commitAllowingStateLoss();
}
public static void show(FragmentManager fm, String mimeType, String displayName) {
final Bundle args = new Bundle();
args.putString(EXTRA_MIME_TYPE, mimeType);
args.putString(EXTRA_DISPLAY_NAME, displayName);
final SaveFragment fragment = new SaveFragment();
fragment.setArguments(args);
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_save, fragment, TAG);
ft.commitAllowingStateLoss();
}
private void replaceFragmentVideoByAudio() {
audioCallFragment = new CallAudioFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, audioCallFragment);
try {
transaction.commitAllowingStateLoss();
} catch (Exception e) {
}
}
private void replaceFragmentAudioByVideo() {
// Hiding controls to let displayVideoCallControlsIfHidden add them plus the callback
videoCallFragment = new CallVideoFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, videoCallFragment);
try {
transaction.commitAllowingStateLoss();
} catch (Exception e) {
}
}
public static void show(FragmentManager fm, ArrayList<DocumentInfo> docs, boolean deleteAfter) {
final Bundle args = new Bundle();
args.putParcelableArrayList(EXTRA_DOC_LIST, docs);
args.putBoolean(EXTRA_DELETE_AFTER, deleteAfter);
final MoveFragment fragment = new MoveFragment();
fragment.setArguments(args);
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_save, fragment, TAG);
ft.commitAllowingStateLoss();
}
public static void show(FragmentManager fm, Intent includeApps) {
final Bundle args = new Bundle();
args.putParcelable(EXTRA_INCLUDE_APPS, includeApps);
final RootsFragment fragment = new RootsFragment();
fragment.setArguments(args);
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_roots, fragment);
ft.commitAllowingStateLoss();
}
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();
}
public static void show(FragmentManager fm, ArrayList<DocumentInfo> docs, boolean deleteAfter) {
final Bundle args = new Bundle();
args.putParcelableArrayList(EXTRA_DOC_LIST, docs);
args.putBoolean(EXTRA_DELETE_AFTER, deleteAfter);
final MoveFragment fragment = new MoveFragment();
fragment.setArguments(args);
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_save, fragment, TAG);
ft.commitAllowingStateLoss();
}
public static void show(FragmentManager fm, RootInfo root) {
final ServerFragment fragment = new ServerFragment();
final Bundle args = new Bundle();
args.putParcelable(EXTRA_ROOT, root);
fragment.setArguments(args);
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_directory, fragment);
ft.commitAllowingStateLoss();
}
/**
* Hides every tab and shows search UI for phone lookup.
*/
private void enterSearchUi() {
if (mSearchFragment == null) {
// We add the search fragment dynamically in the first onLayoutChange() and
// mSearchFragment is set sometime later when the fragment transaction is actually
// executed, which means there's a window when users are able to hit the (physical)
// search key but mSearchFragment is still null.
// It's quite hard to handle this case right, so let's just ignore the search key
// in this case. Users can just hit it again and it will work this time.
return;
}
if (mSearchView == null) {
prepareSearchView();
}
final ActionBar actionBar = getActionBar();
final Tab tab = actionBar.getSelectedTab();
// User can search during the call, but we don't want to remember the status.
if (tab != null && !DialpadFragment.phoneIsInUse()) {
mLastManuallySelectedFragment = tab.getPosition();
}
mSearchView.setQuery(null, true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
updateFakeMenuButtonsVisibility(false);
for (int i = 0; i < TAB_INDEX_COUNT; i++) {
sendFragmentVisibilityChange(i, false /* not visible */ );
}
// Show the search fragment and hide everything else.
mSearchFragment.setUserVisibleHint(true);
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.show(mSearchFragment);
transaction.commitAllowingStateLoss();
mViewPager.setVisibility(View.GONE);
// We need to call this and onActionViewCollapsed() manually, since we are using a custom
// layout instead of asking the search menu item to take care of SearchView.
mSearchView.onActionViewExpanded();
mInSearchUi = true;
}
private void changeFragment(Fragment newFragment) {
hideKeyboard();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.commitAllowingStateLoss();
}
public static void show(FragmentManager fm) {
final ConnectionsFragment fragment = new ConnectionsFragment();
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_directory, fragment, TAG);
ft.commitAllowingStateLoss();
}
public static void show(FragmentManager fm) {
final RecentsCreateFragment fragment = new RecentsCreateFragment();
final FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_directory, fragment);
ft.commitAllowingStateLoss();
}
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();
}