Android Open Source - Do-not-get-annoyed Fragment Provider






From Project

Back to project page Do-not-get-annoyed.

License

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.

Java Source Code

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();
  }

}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java