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

The following examples show how to use android.support.v4.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: FeedManageActivity.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 添加或者显示 fragment
 *
 * @param transaction
 * @param fragment
 */
private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    //设置动画
    transaction.setCustomAnimations(
            R.anim.right_in,
            R.anim.right_out);
    //当前的fragment就是点击切换的目标fragment,则不用操作
    if (currentFragment == fragment) {
        return;
    }

    Fragment willCloseFragment = currentFragment;//上一个要切换掉的碎片
    currentFragment = fragment;//当前要显示的碎片

    if (willCloseFragment != null) {
        transaction.hide(willCloseFragment);
    }
    if (!fragment.isAdded()) { // 如果当前fragment未被添加,则添加到Fragment管理器中
        transaction.add(R.id.fl_main_body, currentFragment).commitAllowingStateLoss();
    } else {
        transaction.show(currentFragment).commitAllowingStateLoss();
    }
}
 
Example 2
Source File: BaseMVPActivity.java    From CoordinatorLayoutExample with Apache License 2.0 6 votes vote down vote up
protected void showFragment(int containerId, Fragment from, Fragment to, String tag) {
    FragmentManager supportFragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = supportFragmentManager.beginTransaction();
    if (!to.isAdded()) {    // 先判断是否被add过
        if (tag != null) {
            transaction.hide(from).add(containerId, to, tag);
        } else {
            transaction.hide(from).add(containerId, to);
        }

        // 隐藏当前的fragment,add下一个到Activity中
    } else {
        transaction.hide(from).show(to); // 隐藏当前的fragment,显示下一个
    }
    transaction.commit();

}
 
Example 3
Source File: MainActivity.java    From BaoKanAndroid with MIT License 6 votes vote down vote up
/**
 * 切换fragment
 *
 * @param from            需要隐藏的fragment
 * @param currentFragment 当前需要显示的fragment
 */
private void switchFragment(Fragment from, Fragment currentFragment) {
    // 不是重复点击才切换
    if (mPreviousFragment != currentFragment) {
        mPreviousFragment = currentFragment;

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (from != null) {
            transaction.hide(from);
        }
        if (currentFragment.isAdded()) {
            transaction.show(currentFragment);
        } else {
            transaction.add(R.id.fl_main_content, currentFragment);
        }
        transaction.commit();
    }
}
 
Example 4
Source File: OptionHandler.java    From mobikul-standalone-pos with MIT License 6 votes vote down vote up
public void addOption(Options options) {
    FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
    Fragment fragment;
    fragment = ((BaseActivity) context).mSupportFragmentManager.findFragmentByTag(AddOptionFragment.class.getSimpleName());
    if (fragment == null)
        fragment = new AddOptionFragment();
    if (!fragment.isAdded()) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("options", options);
        bundle.putBoolean("IS_EDIT", false);
        fragment.setArguments(bundle);
        fragmentTransaction.add(((OptionsActivity) context).binding.optionFl.getId(), fragment, fragment.getClass().getSimpleName());
        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName()).commit();
    }
}
 
Example 5
Source File: CategoryHandler.java    From mobikul-standalone-pos with MIT License 6 votes vote down vote up
public void onClickCategory(Category category) {
    FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
    Fragment fragment;
    fragment = ((BaseActivity) context).mSupportFragmentManager.findFragmentByTag(AddCategoryFragment.class.getSimpleName());
    if (fragment == null)
        fragment = new AddCategoryFragment();
    if (!fragment.isAdded()) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("category", category);
        bundle.putBoolean("edit", true);
        fragment.setArguments(bundle);
        Log.d("name", fragment.getClass().getSimpleName() + "");
        fragmentTransaction.add(((CategoryActivity) context).binding.categoryFl.getId(), fragment, fragment.getClass().getSimpleName());
        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName()).commit();
    }
}
 
Example 6
Source File: TaxActivityHandler.java    From mobikul-standalone-pos with MIT License 6 votes vote down vote up
public void editTaxRate(Tax tax) {
    FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
    Fragment fragment;
    fragment = ((BaseActivity) context).mSupportFragmentManager.findFragmentByTag(AddTaxFragment.class.getSimpleName());
    if (fragment == null)
        fragment = new AddTaxFragment();
    if (!fragment.isAdded()) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("tax", tax);
        bundle.putBoolean("edit", true);
        fragment.setArguments(bundle);
        fragmentTransaction.add(((TaxActivity) context).binding.taxFl.getId(), fragment, fragment.getClass().getSimpleName());
        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName()).commit();
    }
}
 
Example 7
Source File: _Rigger.java    From FragmentRigger with MIT License 6 votes vote down vote up
@Override
public void showFragment(@NonNull Fragment fragment, @IdRes int containerViewId, boolean showRepeatAnim) {
    String fragmentTAG = Rigger.getRigger(fragment).getFragmentTAG();
    if (mStackManager.add(fragmentTAG, containerViewId)) {
        addFragmentWithAnim(fragment, containerViewId);
    }
    String[] fragmentTags = mStackManager.getFragmentTags(containerViewId);
    for (String tag : fragmentTags) {
        Fragment hideFrag = mRiggerTransaction.find(tag);
        if (hideFrag == null) continue;
        hideFrag.setUserVisibleHint(false);
    }
    fragment.setUserVisibleHint(true);
    boolean hidden = fragment.isHidden();
    boolean added = fragment.isAdded();
    if (!added || hidden || showRepeatAnim) {
        mRiggerTransaction.hide(getVisibleFragmentTags(containerViewId));
        showFragmentWithAnim(fragment);
    }
    mRiggerTransaction.commit();
}
 
Example 8
Source File: AddNEditCategoryHandler.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
public boolean isValidated(Category category) {
    category.setDisplayError(true);
    Fragment fragment = ((BaseActivity) context).mSupportFragmentManager.findFragmentByTag(AddCategoryFragment.class.getSimpleName());
    if (fragment != null && fragment.isAdded()) {
        AddCategoryFragment categoryFragment = ((AddCategoryFragment) fragment);
        if (!category.getCategoryNameError().isEmpty()) {
            categoryFragment.binding.categoryName.requestFocus();
            return false;
        }
        category.setDisplayError(false);
        return true;
    }
    return false;
}
 
Example 9
Source File: MainActivity.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
/**
     * 修改显示的内容 不会重新加载
     **/
    public void switchContent(Fragment to) {
        Boolean isAdded = false;
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (getSupportFragmentManager().getFragments() != null) {
                for (Fragment f : getSupportFragmentManager().getFragments()) {
                    if (to.getClass().isAssignableFrom(f.getClass())) {
                        if (!f.isAdded()) {
                            transaction.add(R.id.fl_contain, f, f.getClass().getName());
                        } else {
                            transaction.show(f);
                        }
                        isAdded = true;
                    } else {
                        transaction.hide(f);
                        f.setUserVisibleHint(false);
                    }
                }
            }
            if (!isAdded) {
                if (!to.isAdded()) { // 先判断是否被add过
                    transaction.add(R.id.fl_contain, to, to.getClass().getName()).commitNow();
                } else {
                    transaction.show(to).commitNow(); // 隐藏当前的fragment,显示下一个
                }
            } else {
                transaction.commitNow();
            }
            if (getSupportFragmentManager().findFragmentByTag(to.getClass().getName()) != null) {
                getSupportFragmentManager().findFragmentByTag(to.getClass().getName()).setUserVisibleHint(true);
            }
//            setSelectedFragment((BaseFragment) to);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 10
Source File: ActivityUtils.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/**
 * 对Fragment进行显示隐藏的切换,减少fragment的重复创建
 * @param fragmentManager fragment管理器
 * @param hideFragment  需要隐藏的Fragment
 * @param showFragment  需要显示的Fragment
 * @param frameId   布局FrameLayout的Id
 * @param tag  fragment的唯一tag标识
 */
public static void switchFragment(FragmentManager fragmentManager, Fragment hideFragment, Fragment showFragment, int frameId, String tag) {
    if (fragmentManager != null) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (!showFragment.isAdded()) {
            transaction.hide(hideFragment)
                    .add(frameId, showFragment, tag)
                    .commit();
        } else {
            transaction.hide(hideFragment)
                    .show(showFragment)
                    .commit();
        }
    }
}
 
Example 11
Source File: FragmentStack.java    From fragmentstack with Apache License 2.0 5 votes vote down vote up
private void attachFragment(Fragment fragment, String tag) {
  if (fragment != null) {
    if (fragment.isDetached()) {
      ensureTransaction();

      fragmentTransaction.attach(fragment);
    } else if (!fragment.isAdded()) {
      ensureTransaction();

      fragmentTransaction.add(containerId, fragment, tag);
    }
  }
}
 
Example 12
Source File: MainTimeLineActivity.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
private void initFragments() {
    Fragment timeLineFragment = getMainTimeLineFragment();
    Fragment favFragment = getFavFragment();

    Fragment hotWeiboFragment = getHotWeiboViewPagerFragment();

    Fragment hotHuatiFragment = getHotHuaTiViewPagerFragment();

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    if (!timeLineFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, timeLineFragment, MainTimeLineFragment.class.getName());
        fragmentTransaction.hide(timeLineFragment);
    }

    if (!favFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, favFragment, MyFavListFragment.class.getName());
        fragmentTransaction.hide(favFragment);
    }

    if (!hotWeiboFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, hotWeiboFragment, HotWeiboViewPagerFragment.class.getName());
        fragmentTransaction.hide(hotWeiboFragment);
    }

    if (!hotHuatiFragment.isAdded()) {
        fragmentTransaction.add(R.id.center_frame_layout, hotHuatiFragment, HotHuaTiViewPagerFragment.class.getName());
        fragmentTransaction.hide(hotHuatiFragment);
    }

    if (!fragmentTransaction.isEmpty()) {
        fragmentTransaction.commit();
        getSupportFragmentManager().executePendingTransactions();
    }
}
 
Example 13
Source File: MainActivity.java    From SmartChart with Apache License 2.0 5 votes vote down vote up
/**
 * 添加或者显示 fragment
 *
 * @param transaction
 * @param fragment
 */
protected void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) {
    if (currentFragment == fragment)
        return;

    if (!fragment.isAdded()) { // 如果当前fragment未被添加,则添加到Fragment管理器中
        transaction.hide(currentFragment).add(R.id.main_content, fragment).commitAllowingStateLoss();
    } else {
        transaction.hide(currentFragment).show(fragment).commitAllowingStateLoss();
    }
    currentFragment = (BaseFragment) fragment;
}
 
Example 14
Source File: MainActivity.java    From QuickNote with Apache License 2.0 5 votes vote down vote up
private void switchFragment(Fragment from, Fragment to) {
    if (mCurrentFragment != to) {
        mCurrentFragment = to;
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (!to.isAdded()) {
            ft.hide(from).add(R.id.fl_main_page_content, to).commit();
        } else {
            ft.hide(from).show(to).commit();
        }
    }
}
 
Example 15
Source File: MainActivity.java    From JNChartDemo with Apache License 2.0 5 votes vote down vote up
private void switchFragment(Fragment fragment) {
    if (mContent != fragment) {
        if (!fragment.isAdded()) {//判断fragment是否已经添加过
            //先把当前的fragment隐藏,把用到的fragment添加上去
            getSupportFragmentManager().beginTransaction().hide(mContent).add(R.id.home_container, fragment).commit();
        } else {
            //先把当前的fragment隐藏,把已经添加过的并需要用到的fragment显示出
            getSupportFragmentManager().beginTransaction().hide(mContent).show(fragment).commit();
        }
        mContent = fragment;
    }
}
 
Example 16
Source File: AppFragmentPagerAdapter.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    final long itemId = getItemId(position);

    // Do we already have this fragment?
    // String name = makeFragmentName(container.getId(), itemId);
    String name = getTag(position);
    Fragment fragment = mFragmentManager.findFragmentByTag(name);
    if (fragment != null) {
        if (DEBUG)
            Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
        mCurTransaction.attach(fragment);
    } else {
        fragment = getItem(position);
        if (DEBUG)
            Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
        // mCurTransaction.add(container.getId(), fragment,
        // makeFragmentName(container.getId(), itemId));
        if (!fragment.isAdded())
            mCurTransaction.add(container.getId(), fragment, getTag(position));
        else
            mCurTransaction.show(fragment);
    }
    if (fragment != mCurrentPrimaryItem) {
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
    }

    return fragment;
}
 
Example 17
Source File: FragmentStack.java    From fragmentstack with Apache License 2.0 4 votes vote down vote up
private void removeFragment(Fragment fragment) {
  if (fragment != null && fragment.isAdded()) {
    ensureTransaction();
    fragmentTransaction.remove(fragment);
  }
}
 
Example 18
Source File: MainActivity.java    From YCAudioPlayer with Apache License 2.0 4 votes vote down vote up
private void setHide(FragmentTransaction ft, Fragment fragment) {
    if (fragment.isAdded()) {
        ft.hide(fragment);
    }
}
 
Example 19
Source File: PalmUtils.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
public static boolean isAlive(Fragment fragment) {
    return fragment != null
            && fragment.isAdded()
            && fragment.getActivity() != null
            && !fragment.getActivity().isFinishing();
}
 
Example 20
Source File: SaveHarTask.java    From CapturePacket with MIT License 4 votes vote down vote up
@Override
protected void onPostExecute(final File file) {
    if (mPD != null) {
        mPD.dismiss();
        mPD = null;
    }
    Fragment fragment = mFragmentRef.get();
    if (fragment != null && fragment.isAdded() &&fragment.getView() !=null) {
        Snackbar snackbar;
        if (file != null) {
            snackbar = Snackbar.make(fragment.getView(), "文件保存成功!路径:/sdcard/capture/logs/"+file.getName(), Snackbar.LENGTH_LONG);
            snackbar.setAction("去分享", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Context context = v.getContext();
                        Uri uri;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            uri = FileProvider.getUriForFile(context, context.getPackageName(), file);
                        } else {
                            uri = Uri.fromFile(file);
                        }
                        intent.putExtra(Intent.EXTRA_STREAM, uri);
                        intent.setType("text/plain");
                        context.startActivity(Intent.createChooser(intent,file.getName()));
                    } catch (Exception e) {
                        HLog.e(e);
                    }
                }
            });
        } else {
            snackbar = Snackbar.make(fragment.getView(), "文件保存失败!", Snackbar.LENGTH_SHORT);
        }
        snackbar.show();
    }

}