Java Code Examples for android.app.Fragment#isAdded()

The following examples show how to use android.app.Fragment#isAdded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From GankLock with GNU General Public License v3.0 6 votes vote down vote up
public void replaceFragmentByTag(Fragment fragment) {
    if (mCurrentFragment == null) {
        manager.beginTransaction().add(R.id.main_activity_container, fragment,
            fragment.getClass().getSimpleName()).commit();
    } else {
        if (fragment.isAdded()) {
            manager.beginTransaction().hide(mCurrentFragment).show(fragment).commit();
        } else {
            manager.beginTransaction().hide(mCurrentFragment)
                .add(R.id.main_activity_container, fragment,
                    fragment.getClass().getSimpleName())
                .commit();
        }
    }
    mCurrentFragment = fragment;
}
 
Example 2
Source File: QuizActivity.java    From CoolSignIn with Apache License 2.0 5 votes vote down vote up
public void switchContent(Fragment from, Fragment to) {
    if (mContent != to) {
        mContent = to;
        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(); // 隐藏当前的fragment,add下一个到Activity中
        } else {
            transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
        }
    }
}
 
Example 3
Source File: BaseFragmentUtil.java    From android-commons with Apache License 2.0 5 votes vote down vote up
public static void removeDialogFragment(final String tag,
                                        final FragmentManager fragmentManager) {
  if (fragmentManager == null) {
    return;
  }
  final FragmentTransaction ft = fragmentManager.beginTransaction();
  final Fragment prev = fragmentManager.findFragmentByTag(tag);
  if (prev != null && prev.isAdded()) {
    ft.remove(prev);
  }
  ft.commitAllowingStateLoss();
}
 
Example 4
Source File: FragmentManagerHelper.java    From JianshuApp with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isAdded(Fragment fragment) {
    // FIXME
    return fragment.isAdded() || fragment.getTag() != null || fragment.getId() != 0;
}
 
Example 5
Source File: MainActivity.java    From QuickLyric with GNU General Public License v3.0 4 votes vote down vote up
private void selectItem(int position) {
    FragmentManager fragmentManager = getFragmentManager();
    Fragment newFragment;
    String tag;
    switch (position) {
        default:
            // Lyrics
            tag = LYRICS_FRAGMENT_TAG;
            newFragment = fragmentManager.findFragmentByTag(tag);
            if (newFragment == null || !(newFragment instanceof LyricsViewFragment))
                newFragment = new LyricsViewFragment();
            ((LyricsViewFragment) newFragment).showTransitionAnim = true;
            break;
        case 1:
            // Recent Tracks
            tag = RECENT_TRACKS_FRAGMENT_TAG;
            newFragment = fragmentManager.findFragmentByTag(tag);
            if (newFragment == null || !(newFragment instanceof RecentTracksFragment))
                newFragment = new RecentTracksFragment();
            ((RecentTracksFragment) newFragment).showTransitionAnim = true;
            break;
        case 2:
            // Saved Lyrics
            tag = LOCAL_LYRICS_FRAGMENT_TAG;
            newFragment = fragmentManager.findFragmentByTag(tag);
            if (newFragment == null || !(newFragment instanceof LocalLyricsFragment))
                newFragment = new LocalLyricsFragment();
            ((LocalLyricsFragment) newFragment).showTransitionAnim = true;
            break;
        case 3:
            // Separator
            return;
        case 4:
            // Settings
            if (drawer instanceof DrawerLayout)
                ((DrawerLayout) drawer).closeDrawer(drawerView);
            drawer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivityForResult(settingsIntent, 77);
                }
            }, 250);
            return;
        case 5:
            // Feedback
            return;
        case 6:
            // About Dialog
            if (drawer instanceof DrawerLayout)
                ((DrawerLayout) drawer).closeDrawer(drawerView);
            drawer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
                    startActivityForResult(aboutIntent, 77);
                }
            }, 250);
            return;
    }

    final Fragment activeFragment = getDisplayedFragment(getActiveFragments());
    prepareAnimations(activeFragment);

    // Insert the fragment by replacing any existing fragment
    if (newFragment != activeFragment) {
        if (mActionMode != null)
            mActionMode.finish();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.animator.slide_in_start, R.animator.slide_out_start, R.animator.slide_in_start, R.animator.slide_out_start);
        fragmentTransaction.hide(activeFragment);
        if (newFragment.isAdded())
            fragmentTransaction.show(newFragment);
        else
            fragmentTransaction.add(id.main_fragment_container, newFragment, tag);
        ((CollapsingToolbarLayout) findViewById(R.id.toolbar_layout)).setCollapsedTitleTextColor(Color.WHITE);
        fragmentTransaction.commitAllowingStateLoss();
        if (activeFragment instanceof LyricsViewFragment || newFragment instanceof LyricsViewFragment) {
            final Fragment newFragmentCopy = newFragment;
            activeFragment.getView().postDelayed(() -> {
                if (activeFragment instanceof LyricsViewFragment && activeFragment.getView() != null) {
                    expandToolbar(false);
                    showRefreshFab(false);
                } else if (newFragmentCopy instanceof LyricsViewFragment && activeFragment.getView() != null) {
                    expandToolbar(true);
                    showRefreshFab(true);
                }
            }, getResources().getInteger(android.R.integer.config_longAnimTime));
        }
        MaterialSuggestionsSearchView suggestionsSearchView = findViewById(id.material_search_view);
        if (suggestionsSearchView.isSearchOpen())
            suggestionsSearchView.closeSearch();
    }
    if (drawer instanceof DrawerLayout && (newFragment == activeFragment))
        ((DrawerLayout) drawer).closeDrawer(drawerView);
}