Instantly share code, notes, and snippets.
Created
March 13, 2020 08:50
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save PhamNgocPhi/d39aaad0278bee1600c6eaf394192126 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
public class NavigationManager<T extends BaseFragment> { | |
private FragmentManager fragmentManager; | |
private T currentFragment; | |
private int container; | |
public NavigationManager(FragmentManager fragmentManager, @IdRes int container) { | |
this.fragmentManager = fragmentManager; | |
this.container = container; | |
} | |
private void openFragment(Class<T> fragment, @Nullable Bundle bundle, boolean addToBackStack) { | |
if (currentFragment != null && currentFragment.getClass().getName().equalsIgnoreCase(fragment.getName())) { | |
return; | |
} | |
try { | |
currentFragment = fragment.newInstance(); | |
currentFragment.hideSoftKeyboard(); | |
if (bundle != null) { | |
currentFragment.setArguments(bundle); | |
} | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.replace(container, currentFragment, fragment.getName()); | |
if (addToBackStack) { | |
fragmentTransaction.addToBackStack(fragment.getName()); | |
} | |
fragmentTransaction.commit(); | |
} catch (InstantiationException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void addFragment(Class<T> fragment, @Nullable Bundle bundle, boolean addToBackStack) { | |
if (currentFragment != null && currentFragment.getClass().getName().equalsIgnoreCase(fragment.getName())) { | |
return; | |
} | |
try { | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.hide(currentFragment); | |
currentFragment = fragment.newInstance(); | |
currentFragment.hideSoftKeyboard(); | |
if (bundle != null) { | |
currentFragment.setArguments(bundle); | |
} | |
fragmentTransaction.add(container, currentFragment, fragment.getName()); | |
if (addToBackStack) { | |
fragmentTransaction.addToBackStack(fragment.getName()); | |
} | |
fragmentTransaction.commit(); | |
} catch (InstantiationException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* add fragment to view and BackStack | |
* | |
* @param fragment | |
* @param bundle | |
*/ | |
public void addFragment(Class<T> fragment, @Nullable Bundle bundle) { | |
addFragment(fragment, bundle, true); | |
} | |
/** | |
* add fragment to view but don't add to BackStack | |
* | |
* @param fragment | |
* @param bundle | |
*/ | |
@SuppressWarnings("unchecked") | |
public void addFragmentNoAddToBackStack(Class<T> fragment, @Nullable Bundle bundle) { | |
addFragment(fragment, bundle, false); | |
} | |
/** | |
* Pops all the queued fragments | |
*/ | |
private void popEveryFragment() { | |
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); | |
} | |
/** | |
* replace Fragment and add to back stack | |
*/ | |
public void open(Class<T> fragment, @Nullable Bundle bundle) { | |
openFragment(fragment, bundle, true); | |
} | |
/** | |
* replace Fragment and no add to back stack | |
*/ | |
public void openNoAddToBackStack(Class<T> fragment, @Nullable Bundle bundle) { | |
openFragment(fragment, bundle, false); | |
} | |
/** | |
* clear back stack and open fragment (use replace fragment) as root | |
*/ | |
public void openAsRoot(Class<T> fragment, @Nullable Bundle bundle) { | |
popEveryFragment(); | |
openFragment(fragment, bundle, true); | |
} | |
/** | |
* back to previous fragment | |
*/ | |
@SuppressWarnings("unchecked") | |
public void navigateBack(BaseActivity baseActivity, @Nullable Bundle bundle) { | |
if (baseActivity == null) { | |
return; | |
} | |
int backStackEntryCount = fragmentManager.getBackStackEntryCount(); | |
if (backStackEntryCount <= 1 || (backStackEntryCount == 2 && SplashFragment.class.getName().equals(fragmentManager.getBackStackEntryAt(0).getName()))) { | |
baseActivity.finish(); | |
} else { | |
fragmentManager.popBackStackImmediate(); | |
int currentSize = fragmentManager.getBackStackEntryCount(); | |
String currentFragmentName = fragmentManager.getBackStackEntryAt(currentSize - 1).getName(); | |
currentFragment = (T) fragmentManager.findFragmentByTag(currentFragmentName); | |
if (currentFragment != null && bundle != null) { | |
currentFragment.hideSoftKeyboard(); | |
currentFragment.setArguments(bundle); | |
currentFragment.handleReceivedData(bundle); | |
} | |
if (currentFragment != null && currentFragment.isHidden()) { | |
fragmentManager.beginTransaction().show(currentFragment).commit(); | |
} | |
} | |
} | |
/** | |
* back to specify fragment | |
*/ | |
@SuppressWarnings("unchecked") | |
public void navigateBackTo(BaseActivity baseActivity, Class<T> fragment, @Nullable Bundle bundle) { | |
if (baseActivity == null) { | |
return; | |
} | |
int backStackEntryCount = fragmentManager.getBackStackEntryCount(); | |
if (backStackEntryCount <= 1 || (backStackEntryCount == 2 && SplashFragment.class.getName().equals(fragmentManager.getBackStackEntryAt(0).getName()))) { | |
baseActivity.finish(); | |
} else { | |
String name = fragment.getName(); | |
if (fragmentManager.findFragmentByTag(name) != null) { | |
fragmentManager.popBackStackImmediate(name, 0); | |
int currentSize = fragmentManager.getBackStackEntryCount(); | |
String currentFragmentName = fragmentManager.getBackStackEntryAt(currentSize - 1).getName(); | |
currentFragment = (T) fragmentManager.findFragmentByTag(currentFragmentName); | |
if (currentFragment != null && bundle != null) { | |
currentFragment.hideSoftKeyboard(); | |
currentFragment.setArguments(bundle); | |
currentFragment.handleReceivedData(bundle); | |
} | |
if (currentFragment != null && currentFragment.isHidden()) { | |
fragmentManager.beginTransaction().show(currentFragment).commit(); | |
} | |
} | |
} | |
} | |
/** | |
* get current fragment | |
* | |
* @return current fragment is showing | |
*/ | |
public T getCurrentFragment() { | |
return currentFragment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment