public abstract class BaseControllerActivity extends BaseActivity implements ControllerFragment {
@Override public void addFragment(BaseFragment baseFragment, String tag) {
if (baseFragment != null) {
getFragmentTransaction().
add(R.id.fl_main_cointainer, baseFragment, tag).commitAllowingStateLoss(); }
}
@Override public void addFragment(BaseFragment baseFragment, Bundle args, String tag) {
if (baseFragment != null) {
baseFragment.setArguments(args); FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.add(R.id.fl_main_cointainer, baseFragment, tag); fragmentTransaction.commitAllowingStateLoss(); }
}
private FragmentTransaction getFragmentTransaction() {
return getFm().beginTransaction(); }
public FragmentManager getFm() {
return getSupportFragmentManager(); }
@Override public void removeFragment(String tag) {
BaseFragment baseFragment = findFragmentByTag(tag); if (baseFragment != null) {
getFragmentTransaction().remove(baseFragment).commitAllowingStateLoss(); }
}
@Override public void removeFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
getFragmentTransaction().remove(baseFragment).commitAllowingStateLoss(); }
}
@Override public void showFragment(BaseFragment baseFragment, Bundle args) {
if (baseFragment != null) {
baseFragment.setArguments(args); FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.show(baseFragment); fragmentTransaction.commitAllowingStateLoss(); }
}
@Override public void showFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
getFragmentTransaction().show(baseFragment).commitAllowingStateLoss(); }
}
@Override public void showFragment(String tag) {
BaseFragment baseFragment = findFragmentByTag(tag); if (baseFragment != null) {
getFragmentTransaction().show(baseFragment).commitAllowingStateLoss(); }
}
@Override public void hideFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
getFragmentTransaction().hide(baseFragment).commitAllowingStateLoss(); }
}
@Override public void hideFragment(String tag) {
BaseFragment baseFragment = findFragmentByTag(tag); if (baseFragment != null) {
getFragmentTransaction().hide(baseFragment).commitAllowingStateLoss(); }
}
@Override public void replaceFragment(BaseFragment baseFragment, boolean isAddToBackStack) {
if (baseFragment != null) {
FragmentManager fragmentManager = getFm(); // baseFragment = ((BaseFragment) fragmentManager.findFragmentById(R.id.fl_main_cointainer)); baseFragment.setAddToBackStack(isAddToBackStack); fragmentManager.beginTransaction().replace(R.id.fl_main_cointainer, baseFragment)
.addToBackStack(fragmentManager.findFragmentById(R.id.fl_main_cointainer).toString()
+ "->" + baseFragment.toString()).commitAllowingStateLoss(); }
}
@Override public void replaceFragment(BaseFragment baseFragment, Bundle args, boolean isAddToBackStack) {
if (baseFragment != null) {
FragmentManager fragmentManager = getFm(); // baseFragment = ((BaseFragment) fragmentManager.findFragmentById(R.id.fl_main_cointainer)); baseFragment.setArguments(args); baseFragment.setAddToBackStack(isAddToBackStack); FragmentTransaction fragmentTransaction = getFm().beginTransaction(); fragmentTransaction.replace(R.id.fl_main_cointainer, baseFragment); fragmentTransaction.addToBackStack(baseFragment.getClass().getName()); fragmentTransaction.commitAllowingStateLoss(); }
}
@Override public void replaceFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
FragmentManager fragmentManager = getFm(); fragmentManager.beginTransaction()
.replace(R.id.fl_main_cointainer, baseFragment).commitAllowingStateLoss(); }
}
@Override public void replaceFragment(BaseFragment baseFragment, int id) {
if (baseFragment != null) {
FragmentManager fragmentManager = getFm(); fragmentManager.beginTransaction()
.replace(id, baseFragment).commitAllowingStateLoss(); }
}
@Override public void replaceFragment(BaseFragment baseFragment, Bundle args) {
baseFragment.setArguments(args); FragmentManager fragmentManager = getFm(); fragmentManager.beginTransaction()
.replace(R.id.fl_main_cointainer, baseFragment).commitAllowingStateLoss(); }
@Override public BaseFragment getCurrentlyDisplayedFragment() {
return (BaseFragment) getFm().findFragmentById(R.id.fl_main_cointainer); }
@Override public BaseFragment getCurrentlyDisplayedFragment(int framelayout) {
return (BaseFragment) getFm().findFragmentById(framelayout); }
@Override public BaseFragment findFragmentByTag(String tag) {
return (BaseFragment) getFm().findFragmentByTag(tag); }
@Override public void removeAllFragment() {
FragmentManager fm =getFm(); for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack(); }
}
@Override public void goBack() {
System.out.println(""); if (getFm().getBackStackEntryCount() > 0) {
getFm().popBackStackImmediate(); } else {
super.onBackPressed(); }
}
}
**************************************************************************************
public abstract class BaseControllerActivity extends BaseActivity implements ControllerFragment {
/** * Add Fragment * * @param baseFragment * @param tag */ @Override public void addFragment(BaseFragment baseFragment, String tag) {
if (baseFragment != null) {
FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.
add(R.id.cl_root_view, baseFragment, tag).commitAllowingStateLoss(); }
}
/** * Add new_fragment_invitebymail and pass data using Bundle * * @param baseFragment * @param args * @param tag */ @Override public void addFragment(BaseFragment baseFragment, Bundle args, String tag) {
if (baseFragment != null) {
baseFragment.setArguments(args); FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.add(R.id.cl_root_view, baseFragment, tag); fragmentTransaction.commitAllowingStateLoss(); }
}
/** * Remove Fragment USing TAG * * @param tag */ @Override public void removeFragment(String tag) {
BaseFragment baseFragment = findFragmentByTag(tag); if (baseFragment != null) {
getFragmentTransaction().remove(baseFragment).commitAllowingStateLoss(); }
}
/** * Remove Fragment * * @param baseFragment */
@Override public void removeFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
getFragmentTransaction().remove(baseFragment).commitAllowingStateLoss(); }
}
/** * Show Fragment and send data using Bundle * * @param baseFragment * @param args */ @Override public void showFragment(BaseFragment baseFragment, Bundle args) {
if (baseFragment != null) {
baseFragment.setArguments(args); FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.show(baseFragment); fragmentTransaction.commitAllowingStateLoss(); }
}
/** * Show Fragment * * @param baseFragment */ @Override public void showFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.show(baseFragment).commitAllowingStateLoss(); }
}
/** * Show Fragment Using TAG * * @param tag */ @Override public void showFragment(String tag) {
BaseFragment baseFragment = findFragmentByTag(tag); if (baseFragment != null) {
FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.show(baseFragment).commitAllowingStateLoss(); }
}
/** * Hide Fragment * * @param baseFragment */ @Override public void hideFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
getFragmentTransaction().hide(baseFragment).commitAllowingStateLoss(); }
}
/** * Hide Fragmnet using TAG * * @param tag */ @Override public void hideFragment(String tag) {
BaseFragment baseFragment = findFragmentByTag(tag); if (baseFragment != null) {
getFragmentTransaction().hide(baseFragment).commitAllowingStateLoss(); }
}
/** * Replace Fragment and true if added to backstack and false if not * * @param baseFragment * @param isAddToBackStack */ @Override public void replaceFragment(BaseFragment baseFragment, boolean isAddToBackStack) {
if (baseFragment != null) {
baseFragment.setAddToBackStack(isAddToBackStack); FragmentTransaction fragmentTransaction = getFm().beginTransaction(); fragmentTransaction.replace(R.id.cl_root_view, baseFragment); fragmentTransaction.addToBackStack(baseFragment.getClass().getName()); fragmentTransaction.commitAllowingStateLoss(); }
}
/** * Replace Fragment and true if added to backstack and false if not and send Data using Bundle * * @param baseFragment * @param args * @param isAddToBackStack */ @Override public void replaceFragment(BaseFragment baseFragment, Bundle args, boolean isAddToBackStack) {
if (baseFragment != null) {
baseFragment.setArguments(args); baseFragment.setAddToBackStack(isAddToBackStack); FragmentTransaction fragmentTransaction = getFm().beginTransaction(); fragmentTransaction.replace(R.id.cl_root_view, baseFragment); fragmentTransaction.addToBackStack(baseFragment.getClass().getName()); fragmentTransaction.commitAllowingStateLoss(); }
}
/** * Replace Fragment * * @param baseFragment */ @Override public void replaceFragment(BaseFragment baseFragment) {
if (baseFragment != null) {
FragmentTransaction fragmentTransaction = getFm().beginTransaction(); fragmentTransaction.replace(R.id.cl_root_view, baseFragment); fragmentTransaction.addToBackStack(baseFragment.getClass().getName()); fragmentTransaction.commitAllowingStateLoss(); }
}
/** * Replace Fragment using View ID * * @param baseFragment * @param cointainerviewid */ @Override public void replaceFragment(BaseFragment baseFragment, int cointainerviewid) {
if (baseFragment != null) {
FragmentTransaction fragmentTransaction = getFm().beginTransaction(); fragmentTransaction.replace(cointainerviewid, baseFragment); fragmentTransaction.commitAllowingStateLoss(); }
}
/** * Replace Fragment and send data using Bundle * * @param baseFragment * @param args */ @Override public void replaceFragment(BaseFragment baseFragment, Bundle args) {
baseFragment.setArguments(args); FragmentTransaction fragmentTransaction = getFm().beginTransaction(); fragmentTransaction.replace(R.id.cl_root_view, baseFragment); fragmentTransaction.commitAllowingStateLoss(); }
/** * get Currently displayed Fragment * * @return */ @Override public BaseFragment getCurrentlyDisplayedFragment() {
return (BaseFragment) getFm().findFragmentById(R.id.cl_root_view); }
/** * get Currently displayed Fragment using layout id * * @return */ @Override public BaseFragment getCurrentlyDisplayedFragment(int framelayout) {
return (BaseFragment) getFm().findFragmentById(framelayout); }
/** * get new_fragment_invitebymail using TAG * * @param tag * @return */ @Override public BaseFragment findFragmentByTag(String tag) {
return (BaseFragment) getFm().findFragmentByTag(tag); }
/** * Remove all Fragments */ @Override public void removeAllFragment() {
FragmentManager fm = getFm(); for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack(); }
}
/** * Go back i.e pop fragments */ @Override public void goBack() {
System.out.println(""); if (getFm().getBackStackEntryCount() > 0) {
getFm().popBackStackImmediate(); } else {
super.onBackPressed(); }
}
/** * Get Fragment Transaction * * @return */ private FragmentTransaction getFragmentTransaction() {
return getFm().beginTransaction(); }
/** * Get Fragment Managaer * * @return */ public FragmentManager getFm() {
return getSupportFragmentManager(); }
}
************************************************************************
public interface ControllerFragment {
void addFragment(BaseFragment baseFragment, String tag); void addFragment(BaseFragment baseFragment, Bundle args, String tag);
void removeFragment(String tag);
void removeFragment(BaseFragment baseFragment);
void showFragment(BaseFragment baseFragment); void showFragment(BaseFragment baseFragment, Bundle args);
void showFragment(String tag);
void hideFragment(BaseFragment baseFragment);
void hideFragment(String tag);
void replaceFragment(BaseFragment baseFragment, boolean isAddToBackStack); void replaceFragment(BaseFragment baseFragment, Bundle args, boolean isAddToBackStack);
BaseFragment getCurrentlyDisplayedFragment(); BaseFragment getCurrentlyDisplayedFragment(int framelayout);
BaseFragment findFragmentByTag(String tag);
void goBack();
void replaceFragment(BaseFragment baseFragment); void replaceFragment(BaseFragment baseFragment, int id); void replaceFragment(BaseFragment baseFragment, Bundle args);
void removeAllFragment();}
**********************************************************************************************
basefragment
protected boolean isAddToBackStack = true;public static final String ARG_ADD_TO_BACK_STACK = "is_add_to_backstack";
//Fragmentpublic boolean isAddToBackStack() {
return isAddToBackStack;}
public void setAddToBackStack(boolean addToBackStack) {
isAddToBackStack = addToBackStack;}
public ControllerFragment getNavigationController() {
ControllerFragment navigationController = null;
Fragment parentFragment = getParentFragment(); if (parentFragment != null) {
if (ControllerFragment.class.isAssignableFrom(parentFragment.getClass())) {
navigationController = (ControllerFragment) parentFragment; } else if (BaseFragment.class.isAssignableFrom(parentFragment.getClass())) {
navigationController = ((BaseFragment) parentFragment).getNavigationController(); }
}
if (navigationController == null) {
navigationController = (ControllerFragment) getActivity(); }
return navigationController;}
@Overridepublic void onResume() {
super.onResume(); if (!isAddToBackStack) {
getNavigationController().goBack(); }
}
@Overridepublic void onSaveInstanceState(Bundle outState) {
outState.putBoolean(ARG_ADD_TO_BACK_STACK, isAddToBackStack); super.onSaveInstanceState(outState);}
No comments:
Post a Comment