Q: How to get a LayoutInflater for a fragment out of the OnCreateView method i'm trying to get the LayoutInflater for a fragment  out of the OnCreateView method, i have this method for the activity  
 inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

but i need one for the fragement. i've found some solutions like : 
LayoutInflater inflater = LayoutInflater.from(context);

but how can i define or provide a context.

A: Use getActivity() instead context :
LayoutInflater inflater = LayoutInflater.from(getActivity());

getActivity() : which is represent current activity reference in fragment.

A: 
how can i define or provide a context.

Use getActivity() to get the context of the activity hosting the fragment.

public final Activity getActivity () Added in API level 11
Return the Activity this fragment is currently associated with.


A: use this:
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


A: Define Context in Fragment's OnCreate instead of OnCreateView Method it call only one time When Fragment is Created.
public class MyFragment extends Fragment {

    Context mContext;
    View view;
    LayoutInflater inflaters;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    inflaters = inflater.inflate(R.layout.your_fragment_layout, container, false);
    return view;
    }
}


A: Don't use getActivity().getSystemService since the fragment may not be attached to an activity all the time.
Instead store the inflator object you obtained in onCreateView() to an instance variable.
private LayoutInflator inflator =nulll;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
       this.inflator= inflator;
       View myFragmentView = inflater.inflate(R.layout.your_fragment_layout, container,false);
      return myFragmentView;
    }

public void yourMethodWhereYouNeedInflator()
{
   if(this.inflator!=null)
    {
       View myView = inflater.inflate(R.layout.my_layout);
    }
}

Using parent Activity obtained via getActivity() as Context is not advised for many reasons. It has the potential to cause memory leaks since any View you are going to inflate will hold a reference to the Activity. Any drawable that attached to any such view will also hold reference to the activity. You will soon run into heavy memory leaks if you happen to store such drawable in an global collection. You may use getActivity().getApplicationContext(); but getActivity() will return null if it is called before onAttach() is called by the android runtime. but onCreateView() is guaranteed to be called beforeonAttach(). Hence the better solution is to store the inflater you obtain at onCreateView() into an instance variable;
