Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getActivity(), getApplicationContext() in fragment

what is the correct usage of getActivity() and getApplicationContext() in fragment.

when getting an intent in fragment you use getActivity() and someplaces you use getAppliationContext();

getActivity() and getApplicationContext()

like image 409
griffins Avatar asked Nov 29 '22 21:11

griffins


1 Answers

getActivity()

This method gives the context of the Activity. You can use it is like the yourActivity.this. The method getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated.

getApplicationContext()

Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

For more Info.

like image 80
3iL Avatar answered Dec 04 '22 12:12

3iL