Skip to content

Instantly share code, notes, and snippets.

@melihaksoy
Last active November 12, 2016 09:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melihaksoy/3b83a005a9d307c742cb to your computer and use it in GitHub Desktop.
Save melihaksoy/3b83a005a9d307c742cb to your computer and use it in GitHub Desktop.
An example BaseActivity, which extends AppCompatActivity, to put fragments with animations & start activities with animations easily during development
public class BaseActivity extends AppCompatActivity {
/**
* Puts given fragment to "DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n" id'ed view
*
* @param fragment {@link Fragment} to put in view
*/
public void putFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view and if <code>true</code> adds to backStack with null tag
*
* @param fragment {@link Fragment} to put in view
*/
public void putFragment(Fragment fragment, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(null);
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view and if <code>true</code> adds to backStack with given tag
*
* @param fragment {@link Fragment} to put in view
*/
public void putFragment(Fragment fragment, boolean addToBackStack, String backStackTag) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(backStackTag);
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given tag and if <code>true</code> adds to backStack with given tag
*
* @param fragment {@link Fragment} to put in view
*/
public void putFragment(Fragment fragment, String tag, boolean addToBackStack, String backStackName) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(backStackName);
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment, tag);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given tag
*
* @param fragment {@link Fragment} to put in view
*/
public void putFragment(Fragment fragment, String tag) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment, tag);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given animation
* Uses fragmentTransaction replace method
*
* @param fragment {@link Fragment} to put in view
* @param animationType {@link AnimationType} enum type of the required animation
*/
public void putFragmentWithAnimation(Fragment fragment, AnimationType animationType) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (animationType) {
case SLIDE_IN_RIGHT:
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
break;
case SLIDE_IN_BOTTOM:
ft.setCustomAnimations(R.anim.slide_in_from_bottom, 0, R.anim.slide_out_to_bottom, 0);
break;
case SLIDE_IN_LEFT:
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
break;
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given animation and adds it to back stack if <code>true</code>
* Uses fragmentTransaction replace method
*
* @param fragment {@link Fragment} to put in view
* @param animationType {@link AnimationType} enum type of the required animation
* @param addToBackStack if you want to add fragment to back stack
*/
public void putFragmentWithAnimation(Fragment fragment, AnimationType animationType, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(null);
}
switch (animationType) {
case SLIDE_IN_RIGHT:
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
break;
case SLIDE_IN_BOTTOM:
ft.setCustomAnimations(R.anim.slide_in_from_bottom, 0, R.anim.slide_out_to_bottom, 0);
break;
case SLIDE_IN_LEFT:
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
break;
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given animation and tag
* Uses fragmentTransaction replace method
*
* @param fragment {@link Fragment} to put in view
* @param animationType {@link AnimationType} enum type of the required animation
* @param tag {@link String} replacement tag of the fragment
*/
public void putFragmentWithAnimation(Fragment fragment, AnimationType animationType, String tag) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (animationType) {
case SLIDE_IN_RIGHT:
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
break;
case SLIDE_IN_BOTTOM:
ft.setCustomAnimations(R.anim.slide_in_from_bottom, 0, R.anim.slide_out_to_bottom, 0);
break;
case SLIDE_IN_LEFT:
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
break;
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment, tag);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given animation and tag, adds it to back stack if <code>true</code>
* Uses fragmentTransaction replace method
*
* @param fragment {@link Fragment} to put in view
* @param animationType {@link AnimationType} enum type of the required animation
* @param tag {@link String} replacement tag of the fragment
* @param addToBackStack if you want to add fragment to back stack
*/
public void putFragmentWithAnimation(Fragment fragment, AnimationType animationType, String tag, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(null);
}
switch (animationType) {
case SLIDE_IN_RIGHT:
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
break;
case SLIDE_IN_BOTTOM:
ft.setCustomAnimations(R.anim.slide_in_from_bottom, 0, R.anim.slide_out_to_bottom, 0);
break;
case SLIDE_IN_LEFT:
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
break;
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment, tag);
ft.commit();
}
/**
* Puts given fragment to DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n id'ed view with given animation and tag, adds it to back stack if <code>true</code> with given back stack name
* Uses fragmentTransaction replace method
*
* @param fragment {@link Fragment} to put in view
* @param animationType {@link AnimationType} enum type of the required animation
* @param tag {@link String} replacement tag of the fragment
* @param addToBackStack if you want to add fragment to back stack
* @param backStackName {@link String} Back stack name for the fragment
*/
public void putFragmentWithAnimation(Fragment fragment, AnimationType animationType, String tag, boolean addToBackStack, String backStackName) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(backStackName);
}
switch (animationType) {
case SLIDE_IN_RIGHT:
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
break;
case SLIDE_IN_BOTTOM:
ft.setCustomAnimations(R.anim.slide_in_from_bottom, 0, R.anim.slide_out_to_bottom, 0);
break;
case SLIDE_IN_LEFT:
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
break;
}
ft.replace(R.id.DT410QeExd9KcRDM7n3OItQDo0jxpaT8uOE4n, fragment, tag);
ft.commit();
}
/**
* Starts activity with animation
*
* @param intent {@link Intent} Intent to start
* @param animationType {@link AnimationType} enum type of the required animation
*/
public void startActivityWithAnimation(Intent intent, AnimationType animationType) {
startActivity(intent);
switch (animationType) {
case SLIDE_IN_RIGHT:
overridePendingTransition(R.anim.enter_from_right, R.anim.stay_still);
break;
case SLIDE_IN_BOTTOM:
overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.stay_still);
break;
case SLIDE_IN_LEFT:
overridePendingTransition(R.anim.enter_from_left, R.anim.stay_still);
break;
case SCALE:
overridePendingTransition(R.anim.scale_up, R.anim.scale_down);
break;
}
}
/**
* Starts activity that will return a result state, with animation
*
* @param intent {@link Intent} Intent to start
* @param animationType {@link AnimationType} enum type of the required animation
*/
public void startActivityForResultWithAnimation(Intent intent, int requestCode, AnimationType animationType) {
startActivityForResult(intent, requestCode);
switch (animationType) {
case SLIDE_IN_RIGHT:
overridePendingTransition(R.anim.enter_from_right, R.anim.stay_still);
break;
case SLIDE_IN_BOTTOM:
overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.stay_still);
break;
case SLIDE_IN_LEFT:
overridePendingTransition(R.anim.enter_from_left, R.anim.stay_still);
break;
case SCALE:
overridePendingTransition(R.anim.scale_up, R.anim.scale_down);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment