0

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.

yuva ツ
  • 3,707
  • 9
  • 50
  • 78
K.Ly
  • 75
  • 3
  • 11

5 Answers5

4

Use getActivity() instead context :

LayoutInflater inflater = LayoutInflater.from(getActivity());

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

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
1

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.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

use this:

inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Sats
  • 875
  • 6
  • 12
1

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;
    }
}
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Calling getActivity() inside onCreate is a terrible idea, you have no guarantee the Activity is fully created at that point. You should call that in onActivityCreated... – 2Dee Sep 30 '14 at 12:49
0

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;

Vishnuprasad R
  • 1,682
  • 13
  • 23
  • i want to get it out of the method ! – K.Ly Sep 29 '14 at 09:21
  • @K.Ly Edited my answer to store the inflater on an instance variable; – Vishnuprasad R Sep 29 '14 at 09:47
  • why will `getActivity()` cause memory leaks?. Fragment is attached to the activity. Do read http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Raghunandan Sep 29 '14 at 10:16
  • @Raghunandan I have already explained it in my answer. Fragment is NOT ALWAYS attached to an activity. If you pass an activity context to something, all objects its gonna create (those which require a context) will hold a reference to your activity. This includes drawable that you attach to any view. Drawables will have a callback reference to the view it is attached to, and the view will have reference back to the context you passed in. If the drawable is stored in any container that outlive activity (say a global array) , your activity will never get garbage collected – Vishnuprasad R Sep 29 '14 at 10:37
  • @Raghunandan A fragment's are independent from activity. A fragment may get detached and re-attched to an activity during runtime; – Vishnuprasad R Sep 29 '14 at 10:40
  • If the drawable is stored in any container that outlive activity (say a global array) , your activity will never get garbage collected. That is not always the case. i have use getActivity and mostly the context lives within the activity life cycle – Raghunandan Sep 29 '14 at 10:54