If your activity class extends Activity, you can get application context using getApplicatoinContext(). But this method won’t be available when your Activity extends from Fragment.

Method 1

When your activity extends Fragment, use getActivity () to get the context of the activity.

public class MainActivity extends Fragment {

	protected void onCreate(Bundle savedInstanceState) {

		// Getting application context
		Context context = getActivity();

Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.

  • Eduardo Grotteschi

    Hi Guys, i am stacked in something, let me explain and see if you can help me.

    I started your tutorial about Drawer. This drawer works with a main Activity, and then, each option of the drawer call a Fragment. Up to there all things works great.

    But to populate each fragment, i need to call a php api (also i followed your tutorial) and then programatically build the fragment layout.

    Well, here starts my problem.

    On my fragment code, i create the onCreateView() method and put there the logic of the layout creation. If i run without the php works great (i just create predefined objects). But when i added the logic to go to php and get the info into the hashmap i am starting to face the issues.

    The logic of going to PHP i put it inside the onCreate() method of the fragment. But what i could see is that my hashmap is not ready (or with data) when the fragment starts. It seems is slower and the onCreateView() runs faster than the asyncronic method, so when tries to use the hashmap it said, hey! this variable is null.

    As what i saw in your php example, when you create the asyncronous method to go to php and load the hashmap, then you worked with the onPostExecute to populate the list adapter. But in this case i doesnt have a list adapter, i just create the layout on the fly, i should do the layout there? on the postExecute? (also i dont know why, but android doesnt know what is runOnUiThread).


  • Sora

    How can i pass data from main fragmentactivity to fragments???

  • you may use

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    Bundle b = getActivity().getIntent().getExtras();
    name = b.getString(“name”);
    email = b.getString(“email”);
    View view = inflater.inflate(R.layout.categoryfragment, container, false);
    return view;

    or use Shared preference for that

    • Nagesh

      how can i connect to sqlite db from fragments which are used in sidebar

  • Preeti Maurya

    i am unable to go from a class that extends activity to a class that extends fragmentActivity , pls help me sir. thank you.

  • Rahul Nagure

    Hi sir i m used “Context context=getActivity();”
    but its not working still i hv error in “Intent intent=getIntent();”

    • Manzoor Ahmed

      use it like Context context = getActivity().getApplicationContext();

      getActivity(); returns null when no activity is associated with the fragment, or it is called before onAttach() in fragment.

  • Yogeshwar P

    How to hide an particular fragment tab via validation??while onswipe..

  • Sangita ramar

    how to perform calculation in list of recyclerview , i want to perform sum of all inputs from list and print the values in each list of recyclerview.