Showing posts with label What is Fragment. Show all posts
Showing posts with label What is Fragment. Show all posts

Saturday, July 13, 2013

What is Fragment ? And Start With HelloFragment

Hi guys!!

Today I am going to discuss about Fragment. Now it is available with new API open source library android-support v4 or v7 or v13.  android-support v13 is the recent api library.
So lets start to know about fragment the question is arises first what is Fragment?

What is Fragment in Android?

  • A fragment is a class implementing a portion of an activity.
  • A fragment represents a particular operation or interface running within a larger activity.
  • Fragments must be embedded in activities; they cannot run independent of activities.
  • Most fragments define their own layout of views that live within the activity�s view hierarchy.  
  • A fragment has its own life-cycle, closely related to the life-cycle of its host activity. 
  • A fragment can be a static part of an activity, instantiated automatically during the activity�s creation.
  • Or, you can create, add, and remove fragments dynamically in an activity at run-time.

What is Life cycle of Fragment ?

Fragments have a few extra life cycle callbacks managing interaction with the activity:
onAttach(Activity) => Called when the fragment has been associated with the activity.
onCreateView(LayoutInflater, ViewGroup, Bundle) => Called to create the view hierarchy associated   
 with the fragment.
onActivityCreated(Bundle) => Called when the activity�s onCreate() method has returned.
onDestroyView() => Called when the view hierarchy associated with the fragment is being removed.
onDetach() => Called when the fragment is being disassociated from the activity.


  Fragment Implemented ?

Fragments were added to the Android API in Honeycomb, API 11.
The primary classes related to fragments are:
android.app.Fragment 
android.app.FragmentManager 
android.app.FragmentTransaction
 
The primary classes related to fragments are available in library package are:
android.support.v4.app.FragmentActivity
android.support.v4.app.Fragment
android.support.v4.app.FragmentManager
android.support.v4.app.FragmentTransaction
 
To use the Compatibility Package features, your activity must use android.support.v4.app. FragmentActivity as a base class, rather than android.app.Activity or one of its subclasses. 
 
Lets start the use of this fragment with Simple Hello Fragment. 

main.xml





hello_fragment.xml





HelloFragment

package com.sunil.fragment;

import com.sunil.fragment.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HelloFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
return v;
}

}

MainActivity.java

package com.sunil.fragment;

import com.sunil.fragment.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Cheers Guys!!

Please share this site guys!
KidsMitra

 

Copyright @ 2013 Android Developers Tipss.