Skip to main content

Posts

Showing posts from 2015

Adding fragment without UI

The fragment without UI can only be add within the activity programmatically. For this we have different variation of add method available.Use public abstract  FragmentTransaction  add ( Fragment  frag,  String  tag) frag:  fragment we want  to add in  activity tag: unique ID for identification usually the name of fragment. This method add the fragment in activity .As there is no UI so onCreateView () implementation is not required. Code snippet:             FragmentManager fragmentManager = getFragmentManager();             FragmentTransaction fragmentTransaction = fragmentManager                         .beginTransaction();             // creating a fragment object             Fragmentone fragmentone = new Fragmentone();             fragmentTransaction .add( fragmentone , "fragmentone" );

Fragments

A Fragment represents a behavior or a portion of user interface in an Activity . A fragment must always be embedded in an activity and the fragment's life cycle is directly affected by the host activity's life cycle.  You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup .  We can also use a fragment without its own UI as an invisible worker for the activity.  Fragments can be added, removed, replaced, and animated inside an Activity dynamically.  Fragment API Level Fragment introduced in Honeycomb (Android 3.0) API level 11 Purpose  support more dynamic and flexible UI designs on large screens  promote re usability .  key classes Fragment FragmentManager FragmentTransaction Step of Creating Fragment Step-1 Create a subclass of fragment Step-2 Adding a user interface