Android getting application context in Fragments

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();