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

The following examples show how to use android.support.v4.app.Fragment#isRemoving() . 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: BaseFragment.java    From px-android with MIT License 6 votes vote down vote up
@Nullable
@Override
public Animation onCreateAnimation(final int transit, final boolean enter, final int nextAnim) {
    final Fragment parent = getParentFragment();
    // Apply the workaround only if this is a child fragment, and the parent
    // is being removed.
    if (!enter && parent != null && parent.isRemoving()) {
        // This is a workaround for the bug where child fragments disappear when
        // the parent is removed (as all children are first removed from the parent)
        // See https://code.google.com/p/android/issues/detail?id=55228
        final Animation doNothingAnim = new AlphaAnimation(1, 1);
        doNothingAnim.setStartOffset(getResources().getInteger(R.integer.cf_anim_duration));
        doNothingAnim.setDuration(getResources().getInteger(R.integer.cf_anim_duration));
        return doNothingAnim;
    } else {
        return super.onCreateAnimation(transit, enter, nextAnim);
    }
}
 
Example 2
Source File: ViperHelper.java    From android-viper with Apache License 2.0 6 votes vote down vote up
/**
 * Use in case this Presenter is associated with an {@link Fragment}
 * Call from {@link Fragment#onDestroy()}
 *
 * @param fragment Any instance of type {@link Fragment}
 */
public void onDestroy(@NonNull Fragment fragment)
{
	if(this.mPresenter == null) return;

	if(fragment.getActivity().isFinishing())
	{
		removePresenter();
	}
	else if(fragment.isRemoving() && !this.mOnSaveInstanceCalled)
	{
		// The fragment can be still in backstack even if isRemoving() is true.
		// We check mOnSaveInstanceCalled - if this was not called then the fragment is totally removed.
		Log.d(LOG_TAG, "Removing presenter - fragment replaced");
		removePresenter();
	}
	this.mAlreadyCreated = false;
}
 
Example 3
Source File: FragmentMvpDelegateImpl.java    From mosby with Apache License 2.0 6 votes vote down vote up
static boolean retainPresenterInstance(Activity activity, Fragment fragment,
    boolean keepPresenterInstanceDuringScreenOrientationChanges,
    boolean keepPresenterOnBackstack) {

  if (activity.isChangingConfigurations()) {
    return keepPresenterInstanceDuringScreenOrientationChanges;
  }

  if (activity.isFinishing()) {
    return false;
  }

  if (keepPresenterOnBackstack && BackstackAccessor.isFragmentOnBackStack(fragment)) {
    return true;
  }

  return !fragment.isRemoving();
}
 
Example 4
Source File: FragmentMviDelegateImpl.java    From mosby with Apache License 2.0 6 votes vote down vote up
private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activity activity,
    Fragment fragment) {

  if (activity.isChangingConfigurations()) {
    if (keepPresenterDuringScreenOrientationChange) {
      return true;
    }
    return false;
  }

  if (activity.isFinishing()) {
    return false;
  }

  if (keepPresenterOnBackstack && BackstackAccessor.isFragmentOnBackStack(fragment)) {
    return true;
  }

  return !fragment.isRemoving();
}
 
Example 5
Source File: BaseFragment.java    From SimpleNews with Apache License 2.0 6 votes vote down vote up
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    final Fragment parent = getParentFragment();

    // Apply the workaround only if this is a child fragment, and the parent
    // is being removed.
    if (!enter && parent != null && parent.isRemoving()) {
        // This is a workaround for the bug where child fragments disappear when
        // the parent is removed (as all children are first removed from the parent)
        // See https://code.google.com/p/android/issues/detail?id=55228
        Animation doNothingAnim = new AlphaAnimation(1, 1);
        doNothingAnim.setDuration(getNextAnimationDuration(parent));
        return doNothingAnim;
    } else {
        return super.onCreateAnimation(transit, enter, nextAnim);
    }
}
 
Example 6
Source File: MvpAppCompatDialogFragment.java    From Moxy with MIT License 5 votes vote down vote up
@Override
public void onDestroy() {
	super.onDestroy();

	//We leave the screen and respectively all fragments will be destroyed
	if (getActivity().isFinishing()) {
		getMvpDelegate().onDestroy();
		return;
	}

	// When we rotate device isRemoving() return true for fragment placed in backstack
	// http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving
	if (mIsStateSaved) {
		mIsStateSaved = false;
		return;
	}

	// See https://github.com/Arello-Mobile/Moxy/issues/24
	boolean anyParentIsRemoving = false;
	Fragment parent = getParentFragment();
	while (!anyParentIsRemoving && parent != null) {
		anyParentIsRemoving = parent.isRemoving();
		parent = parent.getParentFragment();
	}

	if (isRemoving() || anyParentIsRemoving) {
		getMvpDelegate().onDestroy();
	}
}
 
Example 7
Source File: MvpAppCompatFragment.java    From Moxy with MIT License 5 votes vote down vote up
@Override
public void onDestroy() {
	super.onDestroy();

	//We leave the screen and respectively all fragments will be destroyed
	if (getActivity().isFinishing()) {
		getMvpDelegate().onDestroy();
		return;
	}

	// When we rotate device isRemoving() return true for fragment placed in backstack
	// http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving
	if (mIsStateSaved) {
		mIsStateSaved = false;
		return;
	}

	// See https://github.com/Arello-Mobile/Moxy/issues/24
	boolean anyParentIsRemoving = false;
	Fragment parent = getParentFragment();
	while (!anyParentIsRemoving && parent != null) {
		anyParentIsRemoving = parent.isRemoving();
		parent = parent.getParentFragment();
	}

	if (isRemoving() || anyParentIsRemoving) {
		getMvpDelegate().onDestroy();
	}
}
 
Example 8
Source File: BaseFragmentActivity.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Forward onActivityResult to child Fragment
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
    if (fragment != null && !fragment.isRemoving()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}
 
Example 9
Source File: AsyncFragmentHandler.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Fragment checkAvailability() {
    Fragment fragment = mReference.get();
    if (fragment == null || fragment.isRemoving() || fragment.getActivity() == null || fragment.getActivity().isFinishing()) {
        return null;
    } else {
        return fragment;
    }
}
 
Example 10
Source File: FragmentBase.java    From tns-core-modules-widgets with Apache License 2.0 5 votes vote down vote up
public Fragment getRemovingParentFragment() {
    Fragment parentFragment = this.getParentFragment();
    while (parentFragment != null && !parentFragment.isRemoving()) {
        parentFragment = parentFragment.getParentFragment();
    }

    return parentFragment;
}
 
Example 11
Source File: FragmentUtils.java    From styT with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull final FragmentManager fragmentManager,
                                        final Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        final int type,
                                        final SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        //LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_HIDE_ADD_FRAGMENT:
            ft.hide(srcFragment);
        case TYPE_ADD_FRAGMENT:
            Fragment fragmentByTag = fragmentManager.findFragmentByTag(name);
            if (fragmentByTag != null) {
                destFragment = fragmentByTag;
            }
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 12
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 4 votes vote down vote up
/**
     * 操作fragment
     *
     * @param fragmentManager fragment管理器
     * @param srcFragment     源fragment
     * @param destFragment    目标fragment
     * @param type            操作类型
     * @param sharedElements  共享元素
     * @return destFragment
     */
    private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                            Fragment srcFragment,
                                            @NonNull Fragment destFragment,
                                            int type,
                                            SharedElement... sharedElements) {
        if (srcFragment == destFragment)
            return null;
        if (srcFragment != null && srcFragment.isRemoving()) {
//            Log.e(srcFragment.getClass().getSimpleName() + " is isRemoving");
            return null;
        }
        String name = destFragment.getClass().getSimpleName();
        Bundle args = destFragment.getArguments();

        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (sharedElements == null || sharedElements.length == 0) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        } else {
            for (SharedElement element : sharedElements) {// 添加共享元素动画
                ft.addSharedElement(element.sharedElement, element.name);
            }
        }
        switch (type) {
            case TYPE_ADD_FRAGMENT:
                if (srcFragment != null)
                    ft.hide(srcFragment);
                if (destFragment.isAdded())
                    break;
                ft.add(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_HIDE))
                    ft.hide(destFragment);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_REMOVE_FRAGMENT:
                ft.remove(destFragment);
                break;
            case TYPE_REMOVE_TO_FRAGMENT:
                List<Fragment> fragments = getFragments(fragmentManager);
                for (int i = fragments.size() - 1; i >= 0; --i) {
                    Fragment fragment = fragments.get(i);
                    if (fragment == destFragment) {
                        if (srcFragment != null)
                            ft.remove(fragment);
                        break;
                    }
                    ft.remove(fragment);
                }
                break;
            case TYPE_REPLACE_FRAGMENT:
                ft.replace(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_POP_ADD_FRAGMENT:
                popFragment(fragmentManager);
                ft.add(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_HIDE_FRAGMENT:
                ft.hide(destFragment);
                break;
            case TYPE_SHOW_FRAGMENT:
                ft.show(destFragment);
                break;
            case TYPE_HIDE_SHOW_FRAGMENT:
                ft.hide(srcFragment).show(destFragment);
                break;
        }
        ft.commitAllowingStateLoss();
        return destFragment;
    }
 
Example 13
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                        Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        int type,
                                        SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_ADD_FRAGMENT:
            if (srcFragment != null) ft.hide(srcFragment);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 14
Source File: FragmentUtils.java    From MVVMArms with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                        Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        int type,
                                        SharedElement... sharedElements) {
    if (srcFragment == destFragment) {
        return null;
    }
    if (srcFragment != null && srcFragment.isRemoving()) {
        Timber.e(srcFragment.getClass().getSimpleName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getSimpleName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        // 添加共享元素动画
        for (SharedElement element : sharedElements) {
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_ADD_FRAGMENT:
            if (srcFragment != null) {
                ft.hide(srcFragment);
            }
            if (destFragment.isAdded()) {
                break;
            }
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) {
                ft.hide(destFragment);
            }
            if (args.getBoolean(ARGS_IS_ADD_STACK)) {
                ft.addToBackStack(name);
            }
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) {
                        ft.remove(fragment);
                    }
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) {
                ft.addToBackStack(name);
            }
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) {
                ft.addToBackStack(name);
            }
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
        default:
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}