Back to project page Do-not-get-annoyed.
The source code is released under:
Apache License
If you think the Android project Do-not-get-annoyed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mn100013d.pmu.services; /*from w ww .ja v a 2 s.c o m*/ import mn100013d.pmu.GameTypeFragment; import mn100013d.pmu.PauseFragment; import mn100013d.pmu.ScoresFragment; import mn100013d.pmu.SettingsFragment; import mn100013d.pmu.StartGameFragment; import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; public class FragmentProvider { public static final int MAIN = 0; public static final int GAMETYPE = 1; public static final int SETTINGS = 2; public static final int PAUSE = 3; public static final int SCORES = 4; private static final String MAIN_FRAGMENT = "mn100013d.pmu.services.MAIN_FRAGMENT"; private static final String GAMETYPE_FRAGMENT = "mn100013d.pmu.services.GAMETYPE_FRAGMENT"; private static final String SETTINGS_FRAGMENT = "mn100013d.pmu.services.SETTINGS_FRAGMENT"; private static final String PAUSE_FRAGMENT = "mn100013d.pmu.services.PAUSE_FRAGMENT"; private static final String SCORES_FRAGMENT = "mn100013d.pmu.services.SCORES_FRAGMENT"; private Context context; private View view; private int containterId; private static FragmentProvider instance; protected FragmentProvider() { } public void clear() { FragmentManager fm = ((FragmentActivity) context) .getSupportFragmentManager(); FragmentTransaction ft = ((FragmentActivity) context) .getSupportFragmentManager().beginTransaction(); Fragment f = fm.findFragmentByTag("initial"); ft.remove(f); ft.commit(); } public static FragmentProvider getInstance() { if (instance == null) { instance = new FragmentProvider(); } return instance; } public void setContext(Context context) { this.context = context; } public void setContainerId(int id){ this.containterId = id; } public void changeFragment(int id) { FragmentManager fm; FragmentTransaction ft; Fragment new_fragment; switch (id) { case MAIN: fm = ((FragmentActivity) context).getSupportFragmentManager(); ft = ((FragmentActivity) context).getSupportFragmentManager() .beginTransaction(); new_fragment = fm.findFragmentByTag(MAIN_FRAGMENT); if (new_fragment == null) new_fragment = new StartGameFragment(); ft.replace(containterId, new_fragment, MAIN_FRAGMENT); ft.addToBackStack(null); ft.commit(); break; case GAMETYPE: fm = ((FragmentActivity) context).getSupportFragmentManager(); ft = ((FragmentActivity) context).getSupportFragmentManager() .beginTransaction(); new_fragment = fm.findFragmentByTag(GAMETYPE_FRAGMENT); if (new_fragment == null) new_fragment = new GameTypeFragment(); ft.replace(containterId, new_fragment, GAMETYPE_FRAGMENT); ft.addToBackStack(null); ft.commit(); break; case SETTINGS: fm = ((FragmentActivity) context).getSupportFragmentManager(); ft = ((FragmentActivity) context).getSupportFragmentManager() .beginTransaction(); new_fragment = fm.findFragmentByTag(SETTINGS_FRAGMENT); if (new_fragment == null) new_fragment = new SettingsFragment(); ft.replace(containterId, new_fragment, SETTINGS_FRAGMENT); ft.addToBackStack(null); ft.commit(); break; case PAUSE: fm = ((FragmentActivity) context).getSupportFragmentManager(); ft = ((FragmentActivity) context).getSupportFragmentManager() .beginTransaction(); new_fragment = fm.findFragmentByTag(PAUSE_FRAGMENT); if (new_fragment == null) new_fragment = new PauseFragment(); ft.replace(containterId, new_fragment, PAUSE_FRAGMENT); ft.addToBackStack(null); ft.commit(); break; case SCORES: fm = ((FragmentActivity) context).getSupportFragmentManager(); ft = ((FragmentActivity) context).getSupportFragmentManager() .beginTransaction(); new_fragment = fm.findFragmentByTag(SCORES_FRAGMENT); if (new_fragment == null) new_fragment = new ScoresFragment(); ft.replace(containterId, new_fragment, SCORES_FRAGMENT); ft.addToBackStack(null); ft.commit(); break; } } public void back() { ((FragmentActivity) context).getSupportFragmentManager() .popBackStackImmediate(); } public void remove(Fragment f){ FragmentTransaction ft = ((FragmentActivity) context) .getSupportFragmentManager().beginTransaction(); ft.remove(f); ft.commit(); } }