Code example for FragmentActivity

Methods: findFragment, findViewById, setContentView, setProgressBarIndeterminateVisibility

0
    super.setContentView(layoutResId);
    onSetContentView(); 
  } 
 
  @Override public void setContentView(View view) {
    super.setContentView(view);
    onSetContentView(); 
  } 
 
  @Override public void setContentView(View view, ViewGroup.LayoutParams params) {
    super.setContentView(view, params);
    onSetContentView(); 
  } 
 
  private void onSetContentView() { 
    setProgressBarIndeterminateVisibility(false); 
    mSetContentViewAlreadyCalled = true;
  } 
 
  /** 
   * Helper method that allows to initialize and add a fragment to activities that usually have 
   * just one single fragment. Fragment is added using its class.getName() as tag. 
   * 
   * @param containerId resource id of the fragment container (must be created through android 
   * resources) 
   * @param fragmentClass the class of the fragment to setup 
   */ 
  void setupBaseFragment(int containerId, Class<? extends Fragment> fragmentClass) {
    setupBaseFragment(containerId, fragmentClass, null);
  } 
 
  /** 
   * Helper method that allows to initialize and add a fragment to activities that usually have 
   * just one single fragment. Fragment is added using its class.getName() as tag. 
   * 
   * @param containerId resource id of the fragment container (must be created through android 
   * resources) 
   * @param fragmentClass the class of the fragment to setup 
   * @param args bundle with the arguments to pass to the fragment 
   */ 
  void setupBaseFragment(int containerId, Class<? extends Fragment> fragmentClass, Bundle args) {
    if (mSetContentViewAlreadyCalled) {
      View view = findViewById(containerId);
      if (!(view instanceof ViewGroup)) {
        throw new IllegalStateException(
            "Since you already called setContentView, it must has a ViewGroup whose id is 'containerId'"); 
      } 
    } else { 
      FrameLayout container = new FrameLayout(this);
      container.setId(containerId);
      setContentView(container);
    } 
 
    // let's check whether fragment is already added 
    Fragment fragment = findFragment(fragmentClass);
    if (fragment == null) {
      // if not, let's create it and add it 
      fragment = Fragment.instantiate(this, fragmentClass.getName(), args);
 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();