Find fragment by id - Android User Interface

Android examples for User Interface:Fragment

Description

Find fragment by id

Demo Code

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class Main {

  @SuppressWarnings("unchecked")
  public static <T extends Fragment> T findFragment(FragmentManager fm,
      Integer id) {//from w w w.  j av a  2 s. co  m
    if (fm == null) {
      return null;
    }
    return (T) fm.findFragmentById(id);
  }

  /**
   * 
   * <b>BuildTime:</b> 2014-8-30<br>
   * <b>Description:</b> Find fragment by tag<br>
   * 
   * @param fm
   * @param tag
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T extends Fragment> T findFragment(FragmentManager fm,
      String tag) {
    if (fm == null) {
      return null;
    }
    return (T) fm.findFragmentByTag(tag);
  }
}

Related Tutorials