Example usage for android.app FragmentTransaction commitAllowingStateLoss

List of usage examples for android.app FragmentTransaction commitAllowingStateLoss

Introduction

In this page you can find the example usage for android.app FragmentTransaction commitAllowingStateLoss.

Prototype

public abstract int commitAllowingStateLoss();

Source Link

Document

Like #commit but allows the commit to be executed after an activity's state is saved.

Usage

From source file:es.rgmf.libresportgps.MainActivity.java

/**
 * This method is called when you chose an option in Navigation Drawer.
 *//*  w  ww.ja  va2 s  .  c  o  m*/
@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    switch (position) {
    case 0:
        transaction.replace(R.id.container, TrackListFragment.newInstance());
        break;
    case 1:
        transaction.replace(R.id.container, DataViewFragment.newInstance());
        break;
    case 2:
        transaction.replace(R.id.container, SettingsFragment.newInstance());
    default:
        break;
    }
    // If it selects an item from menu we need to pop the stack of fragments
    // to
    // remove the possible stacked fragment.
    mFragmentManager.popBackStack();
    transaction.commitAllowingStateLoss();
}

From source file:com.android.mail.browse.ConversationPagerAdapter.java

/**
 * Part of an inelegant dance to clean up restored fragments after realizing
 * we don't want the ViewPager around after all in 2-pane. See docs for
 * {@link ConversationPagerController#killRestoredFragments()} and
 * {@link TwoPaneController#restoreConversation}.
 *///from  w  w  w . ja va 2  s. c o  m
public void killRestoredFragments() {
    if (mRestoredState == null) {
        return;
    }

    FragmentTransaction ft = null;
    for (String key : mRestoredState.keySet()) {
        // WARNING: this code assumes implementation details in
        // FragmentStatePagerAdapter2#restoreState
        if (!key.startsWith(BUNDLE_FRAGMENT_PREFIX)) {
            continue;
        }
        final Fragment f = mFragmentManager.getFragment(mRestoredState, key);
        if (f != null) {
            if (ft == null) {
                ft = mFragmentManager.beginTransaction();
            }
            ft.remove(f);
        }
    }
    if (ft != null) {
        ft.commitAllowingStateLoss();
        mFragmentManager.executePendingTransactions();
    }
    mRestoredState = null;
}

From source file:com.android.contacts.detail.ContactDetailLayoutController.java

private void initialize(Bundle savedState) {
    boolean fragmentsAddedToFragmentManager = true;
    mDetailFragment = (ContactDetailFragment) mFragmentManager
            .findFragmentByTag(ContactDetailViewPagerAdapter.ABOUT_FRAGMENT_TAG);
    mUpdatesFragment = (ContactDetailUpdatesFragment) mFragmentManager
            .findFragmentByTag(ContactDetailViewPagerAdapter.UPDATES_FRAGMENT_TAG);

    // If the detail fragment was found in the {@link FragmentManager} then we don't need to add
    // it again. Otherwise, create the fragments dynamically and remember to add them to the
    // {@link FragmentManager}.
    if (mDetailFragment == null) {
        mDetailFragment = new ContactDetailFragment();
        mUpdatesFragment = new ContactDetailUpdatesFragment();
        fragmentsAddedToFragmentManager = false;
    }// w ww.  j ava 2 s  .  c  o m

    mDetailFragment.setListener(mContactDetailFragmentListener);
    NfcHandler.register(mActivity, mDetailFragment);

    // Read from savedState if possible
    int currentPageIndex = 0;
    if (savedState != null) {
        mContactUri = savedState.getParcelable(KEY_CONTACT_URI);
        mContactHasUpdates = savedState.getBoolean(KEY_CONTACT_HAS_UPDATES);
        currentPageIndex = savedState.getInt(KEY_CURRENT_PAGE_INDEX, 0);
    }

    switch (mLayoutMode) {
    case LayoutMode.VIEW_PAGER_AND_TAB_CAROUSEL: {
        // Inflate 2 view containers to pass in as children to the {@link ViewPager},
        // which will in turn be the parents to the mDetailFragment and mUpdatesFragment
        // since the fragments must have the same parent view IDs in both landscape and
        // portrait layouts.
        mDetailFragmentView = mLayoutInflater.inflate(R.layout.contact_detail_about_fragment_container,
                mViewPager, false);
        mUpdatesFragmentView = mLayoutInflater.inflate(R.layout.contact_detail_updates_fragment_container,
                mViewPager, false);

        mViewPagerAdapter = new ContactDetailViewPagerAdapter();
        mViewPagerAdapter.setAboutFragmentView(mDetailFragmentView);
        mViewPagerAdapter.setUpdatesFragmentView(mUpdatesFragmentView);

        mViewPager.addView(mDetailFragmentView);
        mViewPager.addView(mUpdatesFragmentView);
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOnPageChangeListener(mOnPageChangeListener);

        if (!fragmentsAddedToFragmentManager) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.add(R.id.about_fragment_container, mDetailFragment,
                    ContactDetailViewPagerAdapter.ABOUT_FRAGMENT_TAG);
            transaction.add(R.id.updates_fragment_container, mUpdatesFragment,
                    ContactDetailViewPagerAdapter.UPDATES_FRAGMENT_TAG);
            transaction.commitAllowingStateLoss();
            mFragmentManager.executePendingTransactions();
        }

        mTabCarousel.setListener(mTabCarouselListener);
        mTabCarousel.restoreCurrentTab(currentPageIndex);
        mDetailFragment.setVerticalScrollListener(new VerticalScrollListener(TAB_INDEX_DETAIL));
        mUpdatesFragment.setVerticalScrollListener(new VerticalScrollListener(TAB_INDEX_UPDATES));
        mViewPager.setCurrentItem(currentPageIndex);
        break;
    }
    case LayoutMode.TWO_COLUMN: {
        if (!fragmentsAddedToFragmentManager) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.add(R.id.about_fragment_container, mDetailFragment,
                    ContactDetailViewPagerAdapter.ABOUT_FRAGMENT_TAG);
            transaction.add(R.id.updates_fragment_container, mUpdatesFragment,
                    ContactDetailViewPagerAdapter.UPDATES_FRAGMENT_TAG);
            transaction.commitAllowingStateLoss();
            mFragmentManager.executePendingTransactions();
        }
        break;
    }
    case LayoutMode.FRAGMENT_CAROUSEL:
    case LayoutMode.TWO_COLUMN_FRAGMENT_CAROUSEL: {
        // Add the fragments to the fragment containers in the carousel using a
        // {@link FragmentTransaction} if they haven't already been added to the
        // {@link FragmentManager}.
        if (!fragmentsAddedToFragmentManager) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.add(R.id.about_fragment_container, mDetailFragment,
                    ContactDetailViewPagerAdapter.ABOUT_FRAGMENT_TAG);
            transaction.add(R.id.updates_fragment_container, mUpdatesFragment,
                    ContactDetailViewPagerAdapter.UPDATES_FRAGMENT_TAG);
            transaction.commitAllowingStateLoss();
            mFragmentManager.executePendingTransactions();
        }

        mFragmentCarousel.setFragmentViews((FrameLayoutWithOverlay) mDetailFragmentView,
                (FrameLayoutWithOverlay) mUpdatesFragmentView);
        mFragmentCarousel.setCurrentPage(currentPageIndex);

        break;
    }
    }

    // Setup the layout if we already have a saved state
    if (savedState != null) {
        if (mContactHasUpdates) {
            showContactWithUpdates(false);
        } else {
            showContactWithoutUpdates();
        }
    }
}

From source file:org.proninyaroslav.libretorrent.fragments.MainFragment.java

private void saveTorrentError(Throwable e) {
    if (e == null) {
        return;/*w  w w.j  a v a  2  s.c  o  m*/
    }

    sentError = new Exception(e);

    if (e instanceof FileNotFoundException) {
        ErrorReportAlertDialog errDialog = ErrorReportAlertDialog.newInstance(activity.getApplicationContext(),
                getString(R.string.error), getString(R.string.error_file_not_found_add_torrent),
                Log.getStackTraceString(e), R.style.BaseTheme_Dialog, this);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(errDialog, TAG_SAVE_ERROR_DIALOG);
        ft.commitAllowingStateLoss();

    } else if (e instanceof IOException) {
        ErrorReportAlertDialog errDialog = ErrorReportAlertDialog.newInstance(activity.getApplicationContext(),
                getString(R.string.error), getString(R.string.error_io_add_torrent), Log.getStackTraceString(e),
                R.style.BaseTheme_Dialog, this);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(errDialog, TAG_SAVE_ERROR_DIALOG);
        ft.commitAllowingStateLoss();

    } else if (e instanceof FileAlreadyExistsException) {
        Snackbar.make(coordinatorLayout, R.string.torrent_exist, Snackbar.LENGTH_LONG).show();
    } else {
        ErrorReportAlertDialog errDialog = ErrorReportAlertDialog.newInstance(activity.getApplicationContext(),
                getString(R.string.error), getString(R.string.error_add_torrent), Log.getStackTraceString(e),
                R.style.BaseTheme_Dialog, this);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(errDialog, TAG_SAVE_ERROR_DIALOG);
        ft.commitAllowingStateLoss();
    }
}

From source file:org.proninyaroslav.libretorrent.fragments.MainFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case TORRENT_FILE_CHOOSE_REQUEST:
        if (resultCode == Activity.RESULT_OK) {
            if (data.hasExtra(FileManagerDialog.TAG_RETURNED_PATH)) {
                String path = data.getStringExtra(FileManagerDialog.TAG_RETURNED_PATH);

                try {
                    addTorrentDialog(Uri.fromFile(new File(path)));

                } catch (Exception e) {
                    sentError = e;/*  www  . j  a v a  2s .co  m*/

                    Log.e(TAG, Log.getStackTraceString(e));

                    if (getFragmentManager().findFragmentByTag(TAG_ERROR_OPEN_TORRENT_FILE_DIALOG) == null) {
                        ErrorReportAlertDialog errDialog = ErrorReportAlertDialog.newInstance(
                                activity.getApplicationContext(), getString(R.string.error),
                                getString(R.string.error_open_torrent_file), Log.getStackTraceString(e),
                                R.style.BaseTheme_Dialog, this);

                        FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.add(errDialog, TAG_ERROR_OPEN_TORRENT_FILE_DIALOG);
                        ft.commitAllowingStateLoss();
                    }
                }
            }
        }
        break;
    case ADD_TORRENT_REQUEST:
        if (resultCode == Activity.RESULT_OK) {
            if (data.hasExtra(AddTorrentActivity.TAG_RESULT_TORRENT)) {
                Torrent torrent = data.getParcelableExtra(AddTorrentActivity.TAG_RESULT_TORRENT);
                if (torrent != null) {
                    ArrayList<Torrent> list = new ArrayList<>();
                    list.add(torrent);
                    addTorrentsRequest(list);
                }
            }
        }
        break;
    }
}

From source file:com.android.contacts.activities.DialtactsActivity.java

/**
 * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment
 * should be automatically focused again.
 *//*w w  w. ja v a  2  s  .  c  o m*/
private void exitSearchUi() {
    final ActionBar actionBar = getActionBar();

    // Hide the search fragment, if exists.
    if (mSearchFragment != null) {
        mSearchFragment.setUserVisibleHint(false);

        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.hide(mSearchFragment);
        transaction.commitAllowingStateLoss();
    }

    // We want to hide SearchView and show Tabs. Also focus on previously selected one.
    actionBar.setDisplayShowCustomEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (int i = 0; i < TAB_INDEX_COUNT; i++) {
        sendFragmentVisibilityChange(i, i == mViewPager.getCurrentItem());
    }

    // Before exiting the search screen, reset swipe state.
    mDuringSwipe = false;
    mUserTabClick = false;

    mViewPager.setVisibility(View.VISIBLE);

    hideInputMethod(getCurrentFocus());

    // Request to update option menu.
    invalidateOptionsMenu();

    // See comments in onActionViewExpanded()
    mSearchView.onActionViewCollapsed();
    mInSearchUi = false;
}

From source file:com.android.contacts.activities.DialtactsActivity.java

/**
 * Hides every tab and shows search UI for phone lookup.
 *///from   w w w.  j a v  a  2 s.c o  m
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;
}

From source file:com.chen.mail.ui.OnePaneController.java

@Override
protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) {
    super.showConversation(conversation, inLoaderCallbacks);
    mConversationListVisible = false;//from w w  w  .  j  av a2 s .  co m
    if (conversation == null) {
        transitionBackToConversationListMode();
        return;
    }
    disableCabMode();
    if (ConversationListContext.isSearchResult(mConvListContext)) {
        mViewMode.enterSearchResultsConversationMode();
    } else {
        mViewMode.enterConversationMode();
    }
    final FragmentManager fm = mActivity.getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    // Switching to conversation view is an incongruous transition:
    // we are not replacing a fragment with another fragment as
    // usual. Instead, reveal the heretofore inert conversation
    // ViewPager and just remove the previously visible fragment
    // e.g. conversation list, or possibly label list?).
    final Fragment f = fm.findFragmentById(R.id.content_pane);
    // FragmentManager#findFragmentById can return fragments that are not added to the activity.
    // We want to make sure that we don't attempt to remove fragments that are not added to the
    // activity, as when the transaction is popped off, the FragmentManager will attempt to
    // readd the same fragment twice
    if (f != null && f.isAdded()) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.remove(f);
        ft.commitAllowingStateLoss();
        fm.executePendingTransactions();
    }
    mPagerController.show(mAccount, mFolder, conversation, true /* changeVisibility */);
    onConversationVisibilityChanged(true);
    onConversationListVisibilityChanged(false);
}

From source file:com.android.contacts.activities.DialtactsActivity.java

@Override
public void onAttachFragment(Fragment fragment) {
    // This method can be called before onCreate(), at which point we cannot rely on ViewPager.
    // In that case, we will setup the "current position" soon after the ViewPager is ready.
    final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1;

    if (fragment instanceof DialpadFragment) {
        mDialpadFragment = (DialpadFragment) fragment;
    } else if (fragment instanceof CallLogFragment) {
        mCallLogFragment = (CallLogFragment) fragment;
    } else if (fragment instanceof PhoneFavoriteFragment) {
        mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
        mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
        if (mContactListFilterController != null && mContactListFilterController.getFilter() != null) {
            mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter());
        }//from   w w  w . j  av  a 2 s.co m
    } else if (fragment instanceof PhoneNumberPickerFragment) {
        mSearchFragment = (PhoneNumberPickerFragment) fragment;
        mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener);
        mSearchFragment.setQuickContactEnabled(true);
        mSearchFragment.setDarkTheme(true);
        mSearchFragment.setPhotoPosition(ContactListItemView.PhotoPosition.LEFT);
        mSearchFragment.setUseCallableUri(true);
        if (mContactListFilterController != null && mContactListFilterController.getFilter() != null) {
            mSearchFragment.setFilter(mContactListFilterController.getFilter());
        }
        // Here we assume that we're not on the search mode, so let's hide the fragment.
        //
        // We get here either when the fragment is created (normal case), or after configuration
        // changes.  In the former case, we're not in search mode because we can only
        // enter search mode if the fragment is created.  (see enterSearchUi())
        // In the latter case we're not in search mode either because we don't retain
        // mInSearchUi -- ideally we should but at this point it's not supported.
        mSearchFragment.setUserVisibleHint(false);
        // After configuration changes fragments will forget their "hidden" state, so make
        // sure to hide it.
        if (!mSearchFragment.isHidden()) {
            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.hide(mSearchFragment);
            transaction.commitAllowingStateLoss();
        }
    }
}

From source file:org.ozonecity.gpslogger2.GpsMainActivity.java

private void LoadFragmentView(int position) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    switch (position) {
    case 0:/* w  ww .  j ava2s  .  c  o  m*/
        transaction.replace(R.id.container, GpsSimpleViewFragment.newInstance());
        break;
    case 1:
        transaction.replace(R.id.container, GpsDetailedViewFragment.newInstance());
        break;
    default:
    case 2:
        transaction.replace(R.id.container, GpsBigViewFragment.newInstance());
        break;
    }
    transaction.commitAllowingStateLoss();
}