My current code is kinda atrocious to switch fragments and everything, but I don't know a better way to check to see if the fragment I'm trying to switch to.
private void FragmentChange(Bundle Data) {
SetActionBarButtons(new String[] { "SearchButtonOff", "SearchExtraButtonOff" });
String[] data = Data.getStringArray(ACTIVITY_MAIN.DATA);
String type = data[0];
String fragment = data[1];
if (fragment.equals(POST_MAIN)) {
postmainFragment = new Fragment_PostMain();
mainContainer = postmainFragment;
} else if (fragment.equals(POST_ACCOUNT)) {
postaccountFragment = new Fragment_PostAccount();
mainContainer = postaccountFragment;
} else if (fragment.equals(POST_ACCOUNTLOGIN)) {
postaccountloginFragment = new Fragment_PostAccountLogin();
mainContainer = postaccountloginFragment;
} else if (fragment.equals(POST_LOCATION_MAIN)) {
postlocationmainFragment = new Fragment_PostLocationMain();
mainContainer = postlocationmainFragment;
} else if (fragment.equals(POST_IMAGES)) {
postimagesFragment = new Fragment_PostImages();
mainContainer = postimagesFragment;
} else if (fragment.equals(POST_TEMPLATE)) {
posttemplateFragment = new Fragment_PostTemplate();
mainContainer = posttemplateFragment;
} else if (fragment.equals(POST_LOCATION)) {
postlocationFragment = new Fragment_PostLocation();
mainContainer = postlocationFragment;
} else if (fragment.equals(POST_CATEGORY)) {
postcategoryFragment = new Fragment_PostCategory();
mainContainer = postcategoryFragment;
} else if (fragment.equals(POST_CATEGORY_MAIN)) {
postcategorymainFragment = new Fragment_PostCategoryMain();
mainContainer = postcategorymainFragment;
} else if (fragment.equals(POST_LOCATION_MAIN)) {
postlocationmainFragment = new Fragment_PostLocationMain();
mainContainer = postlocationmainFragment;
} else if (fragment.equals(LOADING)) {
loadingFragment = new Fragment_Loading();
mainContainer = loadingFragment;
} else if (fragment.equals(POST_RESULTS_IMAGEVIEW)) {
postresultsimageviewFragment = new FRAGMENT_POST_RESULTS_IMAGEVIEW();
mainContainer = postresultsimageviewFragment;
} else if (fragment.equals(SEARCH_RESULTS_REPLY)) {
postresultsreplyFragment = new FRAGMENT_SEARCH_RESULTS_REPLY();
mainContainer = postresultsreplyFragment;
}
FragmentTransaction FT = getFragmentManager().beginTransaction();
if (type.equals(FRAGMENTCHANGE_FADEIN)) {
FT.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
} else if (type.equals(FRAGMENTCHANGE_FLIP_FORWARD)) {
FT.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out);
} else if (type.equals(FRAGMENTCHANGE_FLIP_BACK)) {
FT.setCustomAnimations(R.animator.card_flip_left_in, R.animator.card_flip_left_out);
}
if (mainContainer != null) {
FT.replace(R.id.main_fragment, mainContainer).commit();
}
}
Basically, I want to pass a Bundle
down and I set the first part of the bundle as the "type" for the animation, and the fragment as the actual fragment that I'm trying to switch it. Then of course since I have so many fragments it just gets really messy with the if
statements.
Is there a better way to write this so it's not as "ugly"?