-
-
Save WarrenFaith/141608cb164d6fcae0e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentTransaction; | |
import com.project.R; | |
import com.project.fragment.BaseFragment; | |
import com.project.module.DialogNavigation; | |
import com.project.module.Navigation; | |
import java.util.List; | |
/** | |
* @author Martin Breuer | |
*/ | |
public final class FragmentUtils { | |
private FragmentUtils() { | |
// helper class hides constructor | |
} | |
public static void navigateTo(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation) { | |
navigateTo(activity, navigation, false); | |
} | |
public static void navigateTo(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation, final boolean withStateLoss) { | |
navigateTo(activity, navigation, null, withStateLoss); | |
} | |
public static void navigateTo(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation, @Nullable final Bundle bundle) { | |
navigateTo(activity, navigation, bundle, false); | |
} | |
public static void navigateTo(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation, | |
@Nullable final Bundle bundle, final boolean withStateLoss) { | |
AndroidUtils.hideKeyboard(activity); | |
final FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction(); | |
fragmentTransaction.addToBackStack(navigation.getTag()); | |
fragmentTransaction.replace(R.id.main_container, navigation.newInstance(bundle), navigation.getTag()); | |
if (withStateLoss) { | |
fragmentTransaction.commitAllowingStateLoss(); | |
} else { | |
fragmentTransaction.commit(); | |
} | |
} | |
public static void setAsRoot(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation) { | |
setAsRoot(activity, navigation, false); | |
} | |
public static void setAsRoot(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation, final boolean withStateLoss) { | |
final FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction(); | |
clearBackStack(activity.getSupportFragmentManager()); | |
fragmentTransaction.replace(R.id.main_container, navigation.newInstance(), navigation.getTag()); | |
if (withStateLoss) { | |
fragmentTransaction.commitAllowingStateLoss(); | |
} else { | |
fragmentTransaction.commit(); | |
} | |
} | |
public static void restoreOrSetAsRoot(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation) { | |
restoreOrSetAsRoot(activity, navigation, false); | |
} | |
public static void restoreOrSetAsRoot(@NonNull final FragmentActivity activity, @NonNull final Navigation navigation, final boolean withStateLoss) { | |
final FragmentManager manager = activity.getSupportFragmentManager(); | |
int backStackCount = manager.getBackStackEntryCount(); | |
Fragment restoredFragment = null; | |
if (backStackCount > 0) { | |
final String fragmentTag = manager.getBackStackEntryAt(backStackCount - 1).getName(); | |
restoredFragment = manager.findFragmentByTag(fragmentTag); | |
} else { | |
List<Fragment> fragmentList = manager.getFragments(); | |
if (fragmentList != null && fragmentList.size() == 1) { | |
restoredFragment = fragmentList.get(0); | |
} | |
} | |
if (restoredFragment == null) { | |
restoredFragment = manager.findFragmentByTag(navigation.getTag()); | |
} | |
if (restoredFragment == null) { | |
setAsRoot(activity, navigation, true); | |
} else { | |
final FragmentTransaction fragmentTransaction = manager.beginTransaction(); | |
fragmentTransaction.replace(R.id.main_container, restoredFragment, restoredFragment.getClass().getSimpleName()); | |
if (withStateLoss) { | |
fragmentTransaction.commitAllowingStateLoss(); | |
} else { | |
fragmentTransaction.commit(); | |
} | |
} | |
} | |
private static void clearBackStack(@NonNull final FragmentManager manager) { | |
while (manager.popBackStackImmediate()) { | |
// nothing to do | |
} | |
} | |
public static DialogFragment showDialog(@NonNull final FragmentActivity activity, @NonNull final DialogNavigation dialogNavigation) { | |
return dialogNavigation.show(activity.getSupportFragmentManager()); | |
} | |
@Nullable | |
public static BaseFragment getCurrentFragment(FragmentActivity activity) { | |
final FragmentManager manager = activity.getSupportFragmentManager(); | |
int backStackCount = manager.getBackStackEntryCount(); | |
Fragment restoredFragment = null; | |
if (backStackCount > 0) { | |
final String fragmentTag = manager.getBackStackEntryAt(backStackCount - 1).getName(); | |
restoredFragment = manager.findFragmentByTag(fragmentTag); | |
} | |
List<Fragment> fragmentList = manager.getFragments(); | |
if (fragmentList != null && fragmentList.size() > 0) { | |
restoredFragment = fragmentList.get(0); | |
} | |
return (BaseFragment) restoredFragment; | |
} | |
@Nullable | |
public static DialogFragment getCurrentDialogFragment(@NonNull final FragmentActivity activity) { | |
for (Fragment fragment : activity.getSupportFragmentManager().getFragments()) { | |
if (fragment instanceof DialogFragment && fragment.isAdded() && !fragment.isHidden()) { | |
return (DialogFragment) fragment; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment