|
package com.lifeslice.app.navigation; |
|
|
|
import android.support.v4.app.Fragment; |
|
import android.support.v4.app.FragmentManager; |
|
import android.support.v4.app.FragmentTransaction; |
|
import java.util.WeakHashMap; |
|
|
|
public class NavigationManager { |
|
private static final String TAG = NavigationManager.class.getSimpleName(); |
|
private static NavigationManager mInstance = null; |
|
private WeakHashMap<String, Fragment> mFragmentsWeakHashMap; |
|
|
|
private final int MAIN_ACTIVITY_CONTAINER = R.id.mainactivity_fragment_container; |
|
|
|
protected NavigationManager() { |
|
mFragmentsWeakHashMap = new WeakHashMap<>(); |
|
} |
|
|
|
public static synchronized NavigationManager get() { |
|
if (mInstance == null) { |
|
mInstance = new NavigationManager(); |
|
} |
|
|
|
return mInstance; |
|
} |
|
|
|
// custom back press handlings |
|
public boolean proceedOnBackPress() { |
|
return true; |
|
} |
|
|
|
|
|
/** |
|
* Navigate to fragment by tag if default fragment instance will satisfy and store it's instance |
|
* |
|
* @param fragmentManager SupporFragmentManager instance |
|
* @param tag Tag of the fragment to replace |
|
* @param addToBackStack True if fragment should be added to backstack |
|
*/ |
|
public void navigateToFragment(FragmentManager fragmentManager, |
|
String tag, boolean addToBackStack) { |
|
|
|
if (mFragmentsWeakHashMap.get(tag) == null) { |
|
setFragmentByTag(tag, getDefaultFragmentInstance(tag)); |
|
} |
|
|
|
if (getFragmentByTag(tag).isVisible() || getFragmentByTag(tag).isAdded()) return; |
|
|
|
replaceFragment(fragmentManager, getFragmentByTag(tag), tag, addToBackStack); |
|
} |
|
|
|
/** |
|
* Navigate to specified fragment and store it by tag |
|
* |
|
* @param fragmentManager SupporFragmentManager instance |
|
* @param fragment Fragment to replace and store |
|
* @param tag Tag of the fragment to replace |
|
* @param addToBackStack True if fragment should be added to backstack |
|
*/ |
|
public void navigateToFragment(FragmentManager fragmentManager, Fragment fragment, |
|
String tag, boolean addToBackStack) { |
|
|
|
setFragmentByTag(tag, fragment); |
|
|
|
if (getFragmentByTag(tag).isVisible() || getFragmentByTag(tag).isAdded()) return; |
|
|
|
replaceFragment(fragmentManager, getFragmentByTag(tag), tag, addToBackStack); |
|
} |
|
|
|
/** |
|
* Pop backstack to the bottom |
|
* |
|
* @param fragmentManager |
|
*/ |
|
public void clearBackstackInclusive(FragmentManager fragmentManager) { |
|
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); |
|
fragmentManager.executePendingTransactions(); |
|
} |
|
|
|
/** |
|
* Pop backstack to the specified fragment. Fragments above the found by tag fragment will be popped up |
|
* |
|
* @param fragmentManager |
|
* @param fragmentTag |
|
*/ |
|
public void popBackStackToFragment(FragmentManager fragmentManager, String fragmentTag) { |
|
int count = fragmentManager.getBackStackEntryCount(); |
|
while (count > 0) { |
|
String name = fragmentManager.getBackStackEntryAt(count - 1).getName(); |
|
if (name.equals(fragmentTag)) return; |
|
count--; |
|
|
|
fragmentManager.popBackStack(); |
|
fragmentManager.executePendingTransactions(); |
|
} |
|
} |
|
|
|
private Fragment getFragmentByTag(String tag) { |
|
return mFragmentsWeakHashMap.get(tag); |
|
} |
|
|
|
private void setFragmentByTag(String tag, Fragment fragment) { |
|
mFragmentsWeakHashMap.put(tag, fragment); |
|
} |
|
|
|
private void replaceFragment(FragmentManager fm, Fragment fragment, |
|
String tag, boolean addToBackStack) { |
|
|
|
FragmentTransaction transaction = fm.beginTransaction(); |
|
transaction.replace(MAIN_ACTIVITY_CONTAINER, fragment); |
|
if (addToBackStack) transaction.addToBackStack(tag); |
|
transaction.commit(); |
|
|
|
fm.executePendingTransactions(); |
|
} |
|
|
|
// default fragments instances handling |
|
private Fragment getDefaultFragmentInstance(String tag) { |
|
return new Fragment(); |
|
} |
|
} |