Example usage for android.app Fragment isAdded

List of usage examples for android.app Fragment isAdded

Introduction

In this page you can find the example usage for android.app Fragment isAdded.

Prototype

final public boolean isAdded() 

Source Link

Document

Return true if the fragment is currently added to its activity.

Usage

From source file:Main.java

public static void removeDialogFragment(final String tag, final FragmentManager fragmentManager) {
    if (fragmentManager == null) {
        return;//from www .j  a  v  a2s.  c  o  m
    }
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment prev = fragmentManager.findFragmentByTag(tag);
    if (prev != null && prev.isAdded()) {
        ft.remove(prev);
    }
    ft.commitAllowingStateLoss();
}

From source file:Main.java

public static void removeFragment(Activity activity, Fragment fragment) {
    if (activity != null) {
        try {//w  w  w .  j ava2  s  . c o m
            FragmentManager fragmentManager = activity.getFragmentManager();
            fragmentManager.executePendingTransactions();
            if (fragment.isAdded()) {
                FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.remove(fragment);
                ft.commitAllowingStateLoss();
                fragmentManager.executePendingTransactions();
            }
        } catch (Exception e) {
        }
    }
}

From source file:com.alimuzaffar.ramadanalarm.widget.FragmentStatePagerAdapter.java

@Override
public Parcelable saveState() {
    Bundle state = null;//  w w w .j a va 2s  .  c  o m
    if (mSavedState.size() > 0) {
        state = new Bundle();
        Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
        mSavedState.toArray(fss);
        state.putParcelableArray("states", fss);
    }
    for (int i = 0; i < mFragments.size(); i++) {
        Fragment f = mFragments.get(i);
        if (f != null && f.isAdded()) {
            if (state == null) {
                state = new Bundle();
            }
            String key = "f" + i;
            mFragmentManager.putFragment(state, key, f);
        }
    }
    return state;
}

From source file:com.frostwire.android.gui.activities.TransferDetailActivity.java

/**
 * Fragment rotation ends up with initialized detail fragments not added,
 * it seems the SectionsPageAdapter doesn't properly tag the fragments
 * and we have to manually find the corresponding added fragment
 * in the list keep by AbstractFragment's getFragments()
 *
 * We receive a fragment whose .isAdded() method returns false and we
 * look into our tracked list of fragments for an equivalent instance that
 * is marked as added and return it.//from   w  w w . j  a  v  a 2s.c o m
 *
 * We'll then replace that instance in our detailFragments[] array
 */
private Fragment getCorrespondingActiveFragment(AbstractTransferDetailFragment currentFragment) {
    List<Fragment> fragments = getFragments();
    if (fragments.size() > 1) {
        for (Fragment f : fragments) {
            if (f.isAdded() && currentFragment.getClass() == f.getClass()) {
                return f;
            }
        }
    }
    return null;
}

From source file:com.bradbergeron.splitviewcontrollerdemo.activities.MainActivity.java

@Override
public void onDrawerItemSelected(final DrawerFragment.FragmentDrawerItem fragmentDrawerItem) {
    if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
        mDrawerLayout.closeDrawer(Gravity.START);
    }//ww  w.  j ava  2s . co  m

    final FragmentManager fragmentManager = getFragmentManager();
    final String fragmentClassName = fragmentDrawerItem.getFragmentClass().getName();

    Fragment newFragment = fragmentManager.findFragmentByTag(fragmentClassName);

    if (newFragment == null) {
        newFragment = Fragment.instantiate(this, fragmentClassName);
    } else if (newFragment.isAdded()) {
        return;
    } else if (fragmentDrawerItem.isDefaultItem() && fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack("Drawer", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, newFragment, fragmentClassName);

    if (!fragmentDrawerItem.isDefaultItem()) {
        transaction.addToBackStack("Drawer");
    }

    transaction.commit();
}

From source file:com.hufeiya.SignIn.activity.QuizActivity.java

public void switchContent(Fragment from, Fragment to) {
    if (mContent != to) {
        mContent = to;//from   w  w  w  .j  a  v  a  2 s  .  c  o  m
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        //.setCustomAnimations(android.R.anim.fade_in, android.R.anim.slide_out_right);
        if (!to.isAdded()) { // ?add
            transaction.hide(from).add(R.id.content, to).commit(); // ???fragmentaddActivity
        } else {
            transaction.hide(from).show(to).commit(); // ???fragment
        }
    }
}

From source file:com.github.paradam.infinitepager.InfiniteFragmentStatePagerAdapter.java

/**
 * Remove a page for the given position.  The adapter is responsible for removing the view from
 * its container, although it only must ensure this is done by the time it returns from {@link
 * #finishUpdate(android.view.ViewGroup)}.
 *
 * @param container The containing View from which the page will be removed.
 * @param position  The page position to be removed.
 * @param object    The same object that was returned by {@link #instantiateItem(android.view.View, int)}.
 *//*from ww w  .java2 s  .  co  m*/
public void destroyRelativeItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment) object;
    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }
    while (mSavedState.size() <= position) {
        mSavedState.add(null);
        if (mFragments.size() <= position) {
            mFragments.add(null);
        }
    }
    if (fragment != null && fragment.isAdded()) {
        mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
    } else {
        mSavedState.set(position, null);
    }
    mFragments.set(position, null);
    mCurTransaction.remove(fragment);
}

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

@Override
protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) {
    super.showConversation(conversation, inLoaderCallbacks);
    mConversationListVisible = false;//w w  w .  ja v a 2 s .  com
    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.github.paradam.infinitepager.InfiniteFragmentStatePagerAdapter.java

/**
 * Create the page for the given position.  The adapter is responsible for adding the view to
 * the container given here, although it only must ensure this is done by the time it returns
 * from {@link #finishUpdate(android.view.ViewGroup)}.
 *
 * @param container The containing View in which the page will be shown.
 * @param position  The page position to be instantiated.
 * @return Returns an Object representing the new page.  This does not need to be a View, but
 * can be some other container of the page.
 *//*from w w w . j a v a  2 s .c  o  m*/
public Object instantiateRelativeItem(ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do. This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    Fragment f;
    if (mFragments.size() > position) {
        f = mFragments.get(position);
        if (f != null) {
            return f;
        }
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }
    f = getRelativeItem(position);

    if (f.getView() != null) {
        mCurTransaction.remove(f);
    }

    if (mSavedState.size() > position) {
        Fragment.SavedState fss = mSavedState.get(position);
        if (fss != null && !f.isAdded()) {
            f.setInitialSavedState(fss);
        } else if (f.isAdded()) {
            Log.e(TAG, "Error when instantiating Fragment: " + f + ", FragmentSavedState: " + fss
                    + ", fragment already added: " + f.isAdded());
        }
    }
    while (mFragments.size() <= position) {
        mFragments.add(null);
    }

    FragmentCompat.setMenuVisibility(f, false);
    FragmentCompat.setUserVisibleHint(f, false);
    mFragments.set(position, f);
    mCurTransaction.add(container.getId(), f);

    return f;
}

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

@Override
protected void showConversation(Conversation conversation) {
    super.showConversation(conversation);
    // TS: tao.gan 2015-09-21 EMAIL FEATURE-559893 ADD_S
    //we change from ConversationListFragment to ConversationViewFragment, should let the toolbar show
    animateShow(null);/*from  ww  w. j a  va 2 s  .  c  o m*/
    // TS: tao.gan 2015-09-21 EMAIL FEATURE-559893 ADD_E
    mConversationListVisible = false;
    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();
        //TS: jin.dong 2015-8-28 EMAIL BUGFIX-1075110 MOD_S
        try {
            fm.executePendingTransactions();
        } catch (IllegalArgumentException e) {
            LogUtils.d(LOG_TAG, e, "IllegalArgumentException occurred");
        }
        //TS: jin.dong 2015-8-28 EMAIL BUGFIX-1075110 MOD_E
    }
    mPagerController.show(mAccount, mFolder, conversation, true /* changeVisibility */);
    onConversationVisibilityChanged(true);
    onConversationListVisibilityChanged(false);
}